The PeripheralController class. More...
#include <PeripheralController.hpp>
Public Member Functions | |
PeripheralController (int servo_addr, int motor_addr) | |
Constructor for the PeripheralController class. | |
~PeripheralController () override | |
Destructor for the PeripheralController class. | |
int | i2c_smbus_write_byte_data (int file, uint8_t command, uint8_t value) override |
Writes a byte of data to a specific register. | |
int | i2c_smbus_read_byte_data (int file, uint8_t command) override |
Reads a byte of data from a specific register. | |
virtual void | write_byte_data (int fd, int reg, int value) override |
Writes a byte of data to a specific register. | |
virtual int | read_byte_data (int fd, int reg) override |
Reads a byte of data from a specific register. | |
void | set_servo_pwm (int channel, int on_value, int off_value) override |
Sets the PWM of a servo motor. | |
void | set_motor_pwm (int channel, int value) override |
Sets the PWM value for a motor. | |
void | init_servo () override |
Initializes the servo controller. | |
void | init_motors () override |
Initializes the motor controllers. | |
![]() | |
virtual | ~IPeripheralController ()=default |
Private Attributes | |
int | servo_bus_fd_ |
int | motor_bus_fd_ |
int | servo_addr_ |
int | motor_addr_ |
The PeripheralController class.
This class is responsible for controlling the peripherals of the car.
Definition at line 33 of file PeripheralController.hpp.
PeripheralController::PeripheralController | ( | int | servo_addr, |
int | motor_addr ) |
Constructor for the PeripheralController class.
servo_addr | The address of the servo controller. |
motor_addr | The address of the motor controller. |
Initializes the I2C buses and sets the device addresses. Throws an exception if either the servo or motor controller cannot be opened or if the address cannot be set.
Definition at line 59 of file PeripheralController.cpp.
|
override |
Destructor for the PeripheralController class.
Closes the file descriptors for the servo and motor I2C buses, ensuring that resources are properly released when the object is destroyed.
Definition at line 85 of file PeripheralController.cpp.
|
overridevirtual |
Reads a byte of data from a specific register.
file | The file descriptor of the I2C bus. |
command | The register address to read from. |
Implements IPeripheralController.
Definition at line 120 of file PeripheralController.cpp.
|
overridevirtual |
Writes a byte of data to a specific register.
file | The file descriptor for the I2C bus. |
command | The register address to write to. |
value | The byte of data to write to the register. |
std::runtime_error | if the I2C write operation fails. |
Implements IPeripheralController.
Definition at line 99 of file PeripheralController.cpp.
|
overridevirtual |
Initializes the motor controllers.
Sets up the motor controllers, configuring them for 100 Hz PWM operation. The motors are then enabled.
Implements IPeripheralController.
Definition at line 236 of file PeripheralController.cpp.
|
overridevirtual |
Initializes the servo controller.
Configures the servo controller with specific register settings. The method writes a sequence of commands to the servo bus to prepare the controller for operation. It includes setting the mode, setting the prescale value, and enabling the output. Each command is followed by a delay to ensure proper initialization.
Implements IPeripheralController.
Definition at line 213 of file PeripheralController.cpp.
|
overridevirtual |
Reads a byte of data from a specific register.
fd | The file descriptor for the I2C bus. |
reg | The register address to read from. |
std::runtime_error | if the I2C read operation fails. |
Implements IPeripheralController.
Definition at line 158 of file PeripheralController.cpp.
|
overridevirtual |
Sets the PWM value for a motor.
channel | The motor channel to set. |
value | The desired PWM value. |
The value is clamped to [0, 4095] and then written to the motor controller.
Implements IPeripheralController.
Definition at line 195 of file PeripheralController.cpp.
|
overridevirtual |
Sets the PWM of a servo motor.
channel | The channel number of the servo motor to control. |
on_value | The on-time value of the PWM signal (in range 0-4095). |
off_value | The off-time value of the PWM signal (in range 0-4095). |
The on-time value is stored in the first two bytes of the register, and the off-time value is stored in the second two bytes. The actual PWM frequency is 50 Hz.
Implements IPeripheralController.
Definition at line 177 of file PeripheralController.cpp.
|
overridevirtual |
Writes a byte of data to a specific register.
fd | The file descriptor for the I2C bus. |
reg | The register address to write to. |
value | The byte of data to write to the register. |
std::runtime_error | if the I2C write operation fails. |
Implements IPeripheralController.
Definition at line 143 of file PeripheralController.cpp.
|
private |
Definition at line 39 of file PeripheralController.hpp.
|
private |
Definition at line 37 of file PeripheralController.hpp.
|
private |
Definition at line 38 of file PeripheralController.hpp.
|
private |
Definition at line 36 of file PeripheralController.hpp.