Hotwheels-Cluster 1.2
Creation of Cluster APP for SEA:ME project.
 
Loading...
Searching...
No Matches
PeripheralController Class Reference

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.
 
- Public Member Functions inherited from IPeripheralController
virtual ~IPeripheralController ()=default
 

Private Attributes

int servo_bus_fd_
 
int motor_bus_fd_
 
int servo_addr_
 
int motor_addr_
 

Detailed Description

The PeripheralController class.

This class is responsible for controlling the peripherals of the car.

Definition at line 33 of file PeripheralController.hpp.

Constructor & Destructor Documentation

◆ PeripheralController()

PeripheralController::PeripheralController ( int servo_addr,
int motor_addr )

Constructor for the PeripheralController class.

Parameters
servo_addrThe address of the servo controller.
motor_addrThe 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.

◆ ~PeripheralController()

PeripheralController::~PeripheralController ( )
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.

Member Function Documentation

◆ i2c_smbus_read_byte_data()

int PeripheralController::i2c_smbus_read_byte_data ( int file,
uint8_t command )
overridevirtual

Reads a byte of data from a specific register.

Parameters
fileThe file descriptor of the I2C bus.
commandThe register address to read from.
Returns
The byte of data read from the register, or -1 if the operation fails.

Implements IPeripheralController.

Definition at line 120 of file PeripheralController.cpp.

Here is the caller graph for this function:

◆ i2c_smbus_write_byte_data()

int PeripheralController::i2c_smbus_write_byte_data ( int file,
uint8_t command,
uint8_t value )
overridevirtual

Writes a byte of data to a specific register.

Parameters
fileThe file descriptor for the I2C bus.
commandThe register address to write to.
valueThe byte of data to write to the register.
Returns
The result of the write operation (0 on success, -1 on failure).
Exceptions
std::runtime_errorif the I2C write operation fails.

Implements IPeripheralController.

Definition at line 99 of file PeripheralController.cpp.

Here is the caller graph for this function:

◆ init_motors()

void PeripheralController::init_motors ( )
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.

Here is the call graph for this function:

◆ init_servo()

void PeripheralController::init_servo ( )
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.

Here is the call graph for this function:

◆ read_byte_data()

int PeripheralController::read_byte_data ( int fd,
int reg )
overridevirtual

Reads a byte of data from a specific register.

Parameters
fdThe file descriptor for the I2C bus.
regThe register address to read from.
Returns
The byte of data read from the register.
Exceptions
std::runtime_errorif the I2C read operation fails.

Implements IPeripheralController.

Definition at line 158 of file PeripheralController.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_motor_pwm()

void PeripheralController::set_motor_pwm ( int channel,
int value )
overridevirtual

Sets the PWM value for a motor.

Parameters
channelThe motor channel to set.
valueThe 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.

Here is the call graph for this function:

◆ set_servo_pwm()

void PeripheralController::set_servo_pwm ( int channel,
int on_value,
int off_value )
overridevirtual

Sets the PWM of a servo motor.

Parameters
channelThe channel number of the servo motor to control.
on_valueThe on-time value of the PWM signal (in range 0-4095).
off_valueThe 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.

Here is the call graph for this function:

◆ write_byte_data()

void PeripheralController::write_byte_data ( int fd,
int reg,
int value )
overridevirtual

Writes a byte of data to a specific register.

Parameters
fdThe file descriptor for the I2C bus.
regThe register address to write to.
valueThe byte of data to write to the register.
Exceptions
std::runtime_errorif the I2C write operation fails.

Implements IPeripheralController.

Definition at line 143 of file PeripheralController.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

Field Documentation

◆ motor_addr_

int PeripheralController::motor_addr_
private

Definition at line 39 of file PeripheralController.hpp.

◆ motor_bus_fd_

int PeripheralController::motor_bus_fd_
private

Definition at line 37 of file PeripheralController.hpp.

◆ servo_addr_

int PeripheralController::servo_addr_
private

Definition at line 38 of file PeripheralController.hpp.

◆ servo_bus_fd_

int PeripheralController::servo_bus_fd_
private

Definition at line 36 of file PeripheralController.hpp.


The documentation for this class was generated from the following files: