1use zerocopy::little_endian::{F64, U16, U32};
6use zerocopy::{FromBytes, FromZeros, Immutable, IntoBytes, KnownLayout, Unaligned};
7
8pub const GRIPPER_VERSION: u16 = 3;
10
11pub const GRIPPER_COMMAND_PORT: u16 = 1338;
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16#[repr(u16)]
17pub enum GripperCommand {
18 Connect = 0,
20 Homing = 1,
22 Grasp = 2,
24 Move = 3,
26 Stop = 4,
28}
29
30impl GripperCommand {
31 pub const fn to_u16(self) -> u16 {
33 self as u16
34 }
35
36 pub const fn from_u16(v: u16) -> Option<GripperCommand> {
38 Some(match v {
39 0 => GripperCommand::Connect,
40 1 => GripperCommand::Homing,
41 2 => GripperCommand::Grasp,
42 3 => GripperCommand::Move,
43 4 => GripperCommand::Stop,
44 _ => return None,
45 })
46 }
47}
48
49#[derive(Debug, Clone, Copy, PartialEq, Eq)]
51#[repr(u16)]
52pub enum GripperStatus {
53 Success = 0,
55 Fail = 1,
57 Unsuccessful = 2,
60 Aborted = 3,
62}
63
64impl GripperStatus {
65 pub const fn to_u16(self) -> u16 {
67 self as u16
68 }
69
70 pub const fn from_u16(v: u16) -> Option<GripperStatus> {
72 Some(match v {
73 0 => GripperStatus::Success,
74 1 => GripperStatus::Fail,
75 2 => GripperStatus::Unsuccessful,
76 3 => GripperStatus::Aborted,
77 _ => return None,
78 })
79 }
80}
81
82#[derive(Debug, Clone, Copy, PartialEq, Eq)]
84#[repr(u16)]
85pub enum GripperConnectStatus {
86 Success = 0,
88 IncompatibleLibraryVersion = 1,
90}
91
92impl GripperConnectStatus {
93 pub const fn to_u16(self) -> u16 {
95 self as u16
96 }
97
98 pub const fn from_u16(v: u16) -> Option<GripperConnectStatus> {
100 Some(match v {
101 0 => GripperConnectStatus::Success,
102 1 => GripperConnectStatus::IncompatibleLibraryVersion,
103 _ => return None,
104 })
105 }
106}
107
108#[derive(Debug, Clone, Copy, FromBytes, IntoBytes, Immutable, KnownLayout, Unaligned)]
110#[repr(C, packed)]
111pub struct GripperCommandHeader {
112 pub command: U16,
114 pub command_id: U32,
116 pub size: U32,
118}
119
120impl GripperCommandHeader {
121 pub fn new(command: GripperCommand, command_id: u32, size: u32) -> Self {
123 GripperCommandHeader {
124 command: U16::new(command.to_u16()),
125 command_id: U32::new(command_id),
126 size: U32::new(size),
127 }
128 }
129}
130
131#[derive(Debug, Clone, Copy, FromBytes, IntoBytes, Immutable, KnownLayout, Unaligned)]
133#[repr(C, packed)]
134pub struct GripperConnectRequest {
135 pub version: U16,
137 pub udp_port: U16,
139}
140
141impl GripperConnectRequest {
142 pub fn new(version: u16, udp_port: u16) -> Self {
144 GripperConnectRequest {
145 version: U16::new(version),
146 udp_port: U16::new(udp_port),
147 }
148 }
149}
150
151#[derive(Debug, Clone, Copy, FromBytes, IntoBytes, Immutable, KnownLayout, Unaligned)]
153#[repr(C, packed)]
154pub struct GripperConnectResponse {
155 pub status: U16,
157 pub version: U16,
159}
160
161#[derive(Debug, Clone, Copy, FromBytes, IntoBytes, Immutable, KnownLayout, Unaligned)]
163#[repr(C, packed)]
164pub struct GripperMoveRequest {
165 pub width: F64,
167 pub speed: F64,
169}
170
171impl GripperMoveRequest {
172 pub fn new(width: f64, speed: f64) -> Self {
174 GripperMoveRequest {
175 width: F64::new(width),
176 speed: F64::new(speed),
177 }
178 }
179}
180
181#[derive(Debug, Clone, Copy, FromBytes, IntoBytes, Immutable, KnownLayout, Unaligned)]
183#[repr(C, packed)]
184pub struct GraspRequest {
185 pub width: F64,
187 pub epsilon_inner: F64,
189 pub epsilon_outer: F64,
191 pub speed: F64,
193 pub force: F64,
195}
196
197impl GraspRequest {
198 pub fn new(width: f64, epsilon_inner: f64, epsilon_outer: f64, speed: f64, force: f64) -> Self {
200 GraspRequest {
201 width: F64::new(width),
202 epsilon_inner: F64::new(epsilon_inner),
203 epsilon_outer: F64::new(epsilon_outer),
204 speed: F64::new(speed),
205 force: F64::new(force),
206 }
207 }
208}
209
210#[derive(Debug, Clone, Copy, FromBytes, IntoBytes, Immutable, KnownLayout, Unaligned)]
212#[repr(C, packed)]
213pub struct GripperCommandResponse {
214 pub status: U16,
216}
217
218#[derive(Debug, Clone, Copy, FromBytes, IntoBytes, Immutable, KnownLayout, Unaligned)]
220#[repr(C, packed)]
221pub struct GripperState {
222 pub message_id: U32,
224 pub width: F64,
226 pub max_width: F64,
228 pub is_grasped: u8,
230 pub temperature: U16,
232}
233
234impl Default for GripperState {
235 fn default() -> Self {
236 GripperState::new_zeroed()
237 }
238}