Class MotorDriver
Defined in File motor_driver.h
Class Documentation
-
class MotorDriver
Class to handle serial communication with the motor driver It is used to send commands to the motor driver and read encoder values. The use:
Create an instance of the class.
Call Setup() to initialize the serial connection.
Use api to send commands to the motor driver.
Public Types
-
using Encoders = std::array<int, 2>
Type to store the encoder values.
Public Functions
-
MotorDriver() = default
Default constructor.
-
void Setup(const std::string &serial_device, int32_t baud_rate, int32_t timeout_ms)
- Parameters:
serial_device – [in] Path to the serial device(eg. /dev/ttyACM0)
baud_rate – [in] Baud rate of the serial connection(eg. 57600)
timeout_ms – [in] Timeout in milliseconds.
-
void SendEmptyMsg()
Send an empty message to the motor driver. The use of this function is to ensure that the motor driver is ready to receive a new command.
-
Encoders ReadEncoderValues()
Read the encoder values from the motor driver. First value is the left encoder, second value is the right encoder.
- Returns:
The encoder values.
-
void SetMotorValues(int val_1, int val_2)
Set the motor values. The unit of the values is in encoder ticks per revolution.
- Parameters:
val_1 – Value for the first motor.
val_2 – Value for the second motor.
-
void SetPidValues(float k_p, float k_d, float k_i, float k_o)
Set the PID values.
- Parameters:
k_p – Proportional gain.
k_d – Derivative gain.
k_i – Integral gain.
k_o – Offset gain.
-
bool is_connected() const
Check if the serial connection is open.
- Returns:
True if the serial connection is open, false otherwise.
-
std::string SendMsg(const std::string &msg_to_send)
Send a message to the motor driver and read the response. The message is sent with a carriage return appended to it.
- Parameters:
msg_to_send – [in] Message to send to the motor driver.
- Returns:
The response from the motor driver.