Class GPIODriver

Inheritance Relationships

Base Type

Class Documentation

class GPIODriver : public husarion_ugv_hardware_interfaces::GPIODriverInterface

Class responsible for managing GPIO pins on Panther robots, handling tasks such as setting pin values, changing pin directions, monitoring pin events, and more.

Public Functions

GPIODriver(std::vector<GPIOInfo> gpio_info_storage)

Constructs the GPIODriver object with information about GPIO pin configurations. This information is necessary for initializing the GPIO functionality.

Example

An example of constructing the GPIODriver object by providing GPIO pin information:

std::vector<GPIOInfo> gpio_configurations = {
  GPIOInfo{GPIOPin::CHRG_SENSE, gpiod::line::direction::INPUT},
  GPIOInfo{GPIOPin::AUX_PW_EN, gpiod::line::direction::OUTPUT},
  GPIOInfo{GPIOPin::LED_SBC_SEL, gpiod::line::direction::OUTPUT, true,
gpiod::line::value::ACTIVE}
  // ... additional GPIO pin configurations
};
GPIODriver gpio_driver(gpio_configurations);

Note

To enable reading pin values, it is required to enable GPIO monitoring. See GPIOMonitorEnable method for more info.

Parameters:

gpio_info_storage – Vector containing information about GPIO pin configurations.

Throws:

std::runtime_error – if the provided gpio_info_storage vector is empty.

~GPIODriver()

The destructor sets the GPIO pin values back to their initial values to ensure proper cleanup. It then releases the line request and turns off the GPIO monitoring thread.

virtual void GPIOMonitorEnable(const bool use_rt = false, const unsigned gpio_monit_thread_sched_priority = 60) override

Enables GPIO pin state monitoring asynchronously. Optionally, configure the Real-Time (RT) FIFO scheduling policy for the monitor thread.

When called, this method starts the GPIO state monitoring thread asynchronously. If use_rt is set to true, it configures the FIFO RT scheduling policy for the monitor thread with the specified gpio_monit_thread_sched_priority. The default priority is 60.

Note

Calling GPIOMonitorEnable is optional after constructing the driver object. It allows asynchronous monitoring of GPIO pin states. Not invoking this method will result in the lack of functionality to read pin values.

Parameters:
  • use_rt – Whether to configure RT FIFO scheduling policy for the monitor thread. Default is set to false.

  • gpio_monit_thread_sched_priority – Priority for the GPIO monitoring thread. Set within the range of 0-99 to enable and configure the FIFO RT scheduling policy for the monitor thread. This parameter is considered only if use_rt is set to true. The default priority is 60.

virtual void ConfigureEdgeEventCallback(const std::function<void(const GPIOInfo&)> &callback) override

This method sets the provided callback function to be executed upon GPIO edge events.

Example

An example of using this method to bind a member function as a callback:

class MyClass {
public:
  void HandleGPIOEvent(const GPIOInfo & gpio_info) {
    // Handle GPIO event here, i.e:
    std::cout << gpio_info.offset << ":    " << gpio_info.value << std::endl;
  }
};

MyClass my_obj;
GPIODriver gpio_driver;
gpio_driver.GPIOMonitorEnable(true, 50);
gpio_driver.ConfigureEdgeEventCallback(
    std::bind(&MyClass::HandleGPIOEvent, &my_obj, std::placeholders::_1));

Parameters:

callback – The callback function to handle GPIO edge events.

Throws:

std::runtime_error – if GPIO monitor thread is not running.

virtual void ChangePinDirection(const GPIOPin pin, const gpiod::line::direction direction) override

Changes the direction of a specific GPIO pin.

Parameters:
  • pin – GPIOPin to change the direction for.

  • direction – New direction for the pin.

virtual bool IsPinAvailable(const GPIOPin pin) const override

Returns true if a specific pin is configured and stored in GPIO info storage.

Parameters:

pin – The GPIO pin to check availability for

Returns:

true if the pin is available, false otherwise

virtual bool IsPinActive(const GPIOPin pin) override

Checks if a specific GPIO pin is active. This method returns the value stored in the class read during the last edge event.

Parameters:

pin – GPIOPin to check.

Throws:

std::runtime_error – if GPIO monitor thread is not running.

Returns:

True if the pin is active, false otherwise.

virtual bool SetPinValue(const GPIOPin pin, const bool value, const std::chrono::milliseconds &pin_validation_wait_time = std::chrono::milliseconds(0)) override

Sets the value for a specific GPIO pin.

Parameters:
  • pin – GPIOPin to set the value for.

  • value – The boolean value to set for the pin.

  • pin_validation_wait_time – The time duration to wait for the pin value to change before checking if change was successful.

Throws:
  • std::invalid_argument – if trying to set the value for an INPUT pin.

  • std::runtime_error – if changing the GPIO state fails.

Returns:

true if the pin value is successfully set, false otherwise.