Skip to content

Commit 5720fe2

Browse files
Merge pull request #99 from pollen-robotics/CarolinePascal/10_09_2025_reboot_packet
feat(reboot): adding support for reboot packet
2 parents 3a553a4 + bd8289a commit 5720fe2

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

src/dynamixel_protocol/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,17 @@ impl DynamixelProtocolHandler {
118118
}
119119
}
120120

121+
/// Send a reboot instruction.
122+
///
123+
/// Reboot the motor with specified `id`.
124+
/// Returns an [CommunicationErrorKind] if the communication fails.
125+
pub fn reboot(&self, serial_port: &mut dyn serialport::SerialPort, id: u8) -> Result<bool> {
126+
match &self.protocol {
127+
ProtocolKind::V1(p) => p.reboot(serial_port, id),
128+
ProtocolKind::V2(p) => p.reboot(serial_port, id),
129+
}
130+
}
131+
121132
/// Reads raw register bytes.
122133
///
123134
/// Sends a read instruction to the motor and wait for the status packet in response.
@@ -333,6 +344,12 @@ trait Protocol<P: Packet> {
333344
Ok(self.read_status_packet(port, id).is_ok())
334345
}
335346

347+
fn reboot(&self, port: &mut dyn SerialPort, id: u8) -> Result<bool> {
348+
self.send_instruction_packet(port, P::reboot_packet(id).as_ref())?;
349+
350+
Ok(self.read_status_packet(port, id).is_ok())
351+
}
352+
336353
fn read(&self, port: &mut dyn SerialPort, id: u8, addr: u8, length: u8) -> Result<Vec<u8>> {
337354
self.send_instruction_packet(port, P::read_packet(id, addr, length).as_ref())?;
338355
self.read_status_packet(port, id)

src/dynamixel_protocol/packet.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub trait Packet {
1010
fn get_payload_size(header: &[u8]) -> Result<usize>;
1111

1212
fn ping_packet(id: u8) -> Box<dyn InstructionPacket<Self>>;
13+
fn reboot_packet(id: u8) -> Box<dyn InstructionPacket<Self>>;
1314

1415
fn read_packet(id: u8, addr: u8, length: u8) -> Box<dyn InstructionPacket<Self>>;
1516
fn write_packet(id: u8, addr: u8, data: &[u8]) -> Box<dyn InstructionPacket<Self>>;

src/dynamixel_protocol/v1.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ impl Packet for PacketV1 {
2323
})
2424
}
2525

26+
fn reboot_packet(id: u8) -> Box<dyn InstructionPacket<Self>> {
27+
Box::new(InstructionPacketV1 {
28+
id,
29+
instruction: InstructionKindV1::Reboot,
30+
params: vec![],
31+
})
32+
}
33+
2634
fn read_packet(id: u8, addr: u8, length: u8) -> Box<dyn InstructionPacket<Self>> {
2735
Box::new(InstructionPacketV1 {
2836
id,
@@ -222,6 +230,7 @@ pub(crate) enum InstructionKindV1 {
222230
Ping,
223231
Read,
224232
Write,
233+
Reboot,
225234
SyncWrite,
226235
SyncRead,
227236
}
@@ -232,6 +241,7 @@ impl InstructionKindV1 {
232241
InstructionKindV1::Ping => 0x01,
233242
InstructionKindV1::Read => 0x02,
234243
InstructionKindV1::Write => 0x03,
244+
InstructionKindV1::Reboot => 0x08,
235245
InstructionKindV1::SyncRead => 0x82,
236246
InstructionKindV1::SyncWrite => 0x83,
237247
}
@@ -261,6 +271,13 @@ mod tests {
261271
assert_eq!(bytes, [0xFF, 0xFF, 0x01, 0x02, 0x01, 0xFB]);
262272
}
263273

274+
#[test]
275+
fn create_reboot_packet() {
276+
let p = PacketV1::reboot_packet(2);
277+
let bytes = p.to_bytes();
278+
assert_eq!(bytes, [0xFF, 0xFF, 0x02, 0x02, 0x08, 0xF3]);
279+
}
280+
264281
#[test]
265282
fn create_read_packet() {
266283
let p = PacketV1::read_packet(1, 0x2B, 1);

src/dynamixel_protocol/v2.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ impl Packet for PacketV2 {
3939
})
4040
}
4141

42+
fn reboot_packet(id: u8) -> Box<dyn InstructionPacket<Self>> {
43+
Box::new(InstructionPacketV2 {
44+
id,
45+
instruction: InstructionKindV2::Reboot,
46+
params: vec![],
47+
})
48+
}
49+
4250
fn read_packet(id: u8, addr: u8, length: u8) -> Box<dyn InstructionPacket<Self>> {
4351
Box::new(InstructionPacketV2 {
4452
id,
@@ -214,6 +222,7 @@ pub(crate) enum InstructionKindV2 {
214222
Ping,
215223
Read,
216224
Write,
225+
Reboot,
217226
SyncRead,
218227
SyncWrite,
219228
}
@@ -224,6 +233,7 @@ impl InstructionKindV2 {
224233
InstructionKindV2::Ping => 0x01,
225234
InstructionKindV2::Read => 0x02,
226235
InstructionKindV2::Write => 0x03,
236+
InstructionKindV2::Reboot => 0x08,
227237
InstructionKindV2::SyncRead => 0x82,
228238
InstructionKindV2::SyncWrite => 0x83,
229239
}
@@ -310,6 +320,7 @@ mod tests {
310320

311321
assert_eq!(crc.to_le_bytes(), [0x16, 0xd2]);
312322
}
323+
313324
#[test]
314325
fn create_ping_packet() {
315326
let p = PacketV2::ping_packet(2);
@@ -320,6 +331,16 @@ mod tests {
320331
);
321332
}
322333

334+
#[test]
335+
fn create_reboot_packet() {
336+
let p = PacketV2::reboot_packet(2);
337+
let bytes = p.to_bytes();
338+
assert_eq!(
339+
bytes,
340+
[0xff, 0xff, 0xfd, 0x0, 0x2, 0x3, 0x0, 0x8, 0x2f, 0x72]
341+
);
342+
}
343+
323344
#[test]
324345
fn create_read_packet() {
325346
let p = PacketV2::read_packet(1, 0x2B, 2);

0 commit comments

Comments
 (0)