dynamixel2/instructions/clear.rs
1use super::{instruction_id, packet_id};
2use crate::{Bus, Response};
3
4/// The parameters for the CLEAR command to clear the revolution counter.
5const CLEAR_REVOLUTION_COUNT: [u8; 5] = [0x01, 0x44, 0x58, 0x4C, 0x22];
6
7impl<ReadBuffer, WriteBuffer> Bus<ReadBuffer, WriteBuffer>
8where
9 ReadBuffer: AsRef<[u8]> + AsMut<[u8]>,
10 WriteBuffer: AsRef<[u8]> + AsMut<[u8]>,
11{
12 /// Clear the multi-revolution counter of a motor.
13 ///
14 /// This will reset the "present position" register to a value between 0 and a whole revolution.
15 /// It is not possible to clear the revolution counter of a motor while it is moving.
16 /// Doing so will cause the motor to return an error, and the revolution counter will not be reset.
17 ///
18 /// You may specify [`crate::instructions::packet_id::BROADCAST`] as motor ID.
19 /// If you do, none of the devices will reply with a response, and this function will not wait for any.
20 ///
21 /// If you want to broadcast this instruction, it may be more convenient to use [`Self::broadcast_clear_revolution_counter()`] instead.
22 pub fn clear_revolution_counter(&mut self, motor_id: u8) -> Result<Response<()>, crate::TransferError> {
23 self.write_instruction(motor_id, instruction_id::CLEAR, CLEAR_REVOLUTION_COUNT.len(), encode_parameters)?;
24 Ok(super::read_response_if_not_broadcast(self, motor_id)?)
25 }
26
27 /// Clear the revolution counter of all connected motors.
28 ///
29 /// This will reset the "present position" register to a value between 0 and a whole revolution.
30 /// It is not possible to clear the mutli-revolution counter of a motor while it is moving.
31 /// Doing so will cause the motor to return an error, and the revolution counter will not be reset.
32 pub fn broadcast_clear_revolution_counter(&mut self) -> Result<(), crate::WriteError> {
33 self.write_instruction(
34 packet_id::BROADCAST,
35 instruction_id::CLEAR,
36 CLEAR_REVOLUTION_COUNT.len(),
37 encode_parameters,
38 )?;
39 Ok(())
40 }
41}
42
43fn encode_parameters(buffer: &mut [u8]) {
44 buffer.copy_from_slice(&CLEAR_REVOLUTION_COUNT)
45}