Skip to content

Commit 4177c90

Browse files
committed
refactor: simplify libgpiod implementation
1 parent 0a08d87 commit 4177c90

3 files changed

Lines changed: 16 additions & 48 deletions

File tree

include/sensor_trigger/jetson_gpio.hpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525

2626
#define SYSFS_GPIO_DIR "/sys/class/gpio"
2727
#define BUFFER_SIZE 64
28-
#define GPIO_OUTPUT 1
29-
#define GPIO_INPUT 0
28+
#define GPIO_OUTPUT gpiod::line_request::DIRECTION_OUTPUT
29+
#define GPIO_INPUT gpiod::line_request::DIRECTION_INPUT
3030
#define GPIO_HIGH 1
3131
#define GPIO_LOW 0
3232

@@ -35,29 +35,22 @@ typedef int gpio_state;
3535

3636
namespace jetson_gpio
3737
{
38-
// Mapping of GPIO number to pin number for ROSCubeX
39-
// Note: pin 5->216 is pin 5 on the DB50 connector, run by GPIO chip 216 (starting at GPIO number
40-
// 216)
41-
static std::map<int, int> pin_gpio_mapping{{5, 216}, {51, 408}, {52, 350}, {53, 446}, {54, 445}};
42-
4338
class JetsonGpio
4439
{
4540
public:
46-
JetsonGpio() : state_file_descriptor_(-1) {}
41+
JetsonGpio() = default;
4742
~JetsonGpio();
4843
bool init_gpio_pin(unsigned int gpio_chip, unsigned int gpio_line, gpio_direction direction);
4944
bool set_gpio_pin_state(gpio_state state);
5045

5146
protected:
52-
bool export_gpio();
53-
bool unexport_gpio();
47+
bool close_gpio();
5448
bool set_gpio_direction(gpio_direction direction);
5549

56-
int state_file_descriptor_;
5750
int gpio_;
5851

5952
gpiod::chip gpio_chip_;
60-
gpiod::line_bulk gpio_lines_;
53+
gpiod::line gpio_line_;
6154
gpiod::line_request gpio_request_;
6255
};
6356
} // namespace jetson_gpio

src/jetson_gpio.cpp

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,11 @@ namespace jetson_gpio
2222
{
2323
JetsonGpio::~JetsonGpio()
2424
{
25-
if (state_file_descriptor_ > -1) {
26-
close(state_file_descriptor_);
27-
}
28-
2925
// Regardless of the existence of other processes that uses the same GPIO pin
3026
// (incl. zombie GPIO port opening because of failure exit),
31-
// this unexport closes the target GPIO pin anyway.
27+
// this function closes the target GPIO pin anyway.
3228
// This behavior intends to make next try to use this GPIO pin success.
33-
unexport_gpio();
29+
close_gpio();
3430
}
3531

3632
bool JetsonGpio::init_gpio_pin(
@@ -39,49 +35,29 @@ bool JetsonGpio::init_gpio_pin(
3935
std::string gpio_character_device = "/dev/gpiochip" + std::to_string(gpio_chip);
4036

4137
gpio_chip_ = gpiod::chip(gpio_character_device);
42-
gpio_lines_ = gpio_chip_.get_lines(
43-
std::vector<unsigned int>({gpio_line})); // XXX: 143 = Anvil misc.I/O GP_Out_1, 108 = PWM_Out_0
38+
gpio_line_ = gpio_chip_.get_line(gpio_line);
4439
gpio_request_ = {
45-
"sensor_trigger", // consumer name. XXX: fixed name may conflict for multiple instances
46-
gpiod::line_request::DIRECTION_OUTPUT, // request_type
47-
0 // flag
40+
"sensor_trigger", // consumer name
41+
direction, // request_type
42+
0 // flag
4843
};
4944

50-
if (!set_gpio_direction(direction)) {
51-
return false;
52-
}
5345

54-
gpio_lines_.request(gpio_request_, std::vector<int>({GPIO_LOW}));
46+
gpio_line_.request(gpio_request_, GPIO_LOW);
5547

5648
return true;
5749
}
5850

59-
bool JetsonGpio::export_gpio() { return true; }
60-
61-
bool JetsonGpio::unexport_gpio()
51+
bool JetsonGpio::close_gpio()
6252
{
6353
gpio_chip_.~chip();
6454

6555
return true;
6656
}
6757

68-
bool JetsonGpio::set_gpio_direction(gpio_direction direction)
69-
{
70-
switch (direction) {
71-
case GPIO_INPUT:
72-
gpio_request_.request_type = gpiod::line_request::DIRECTION_INPUT;
73-
break;
74-
case GPIO_OUTPUT:
75-
gpio_request_.request_type = gpiod::line_request::DIRECTION_OUTPUT;
76-
break;
77-
}
78-
79-
return true;
80-
}
81-
8258
bool JetsonGpio::set_gpio_pin_state(gpio_state state)
8359
{
84-
gpio_lines_.set_values(std::vector<int>({state}));
60+
gpio_line_.set_value(state);
8561

8662
return true;
8763
}

src/sensor_trigger.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,13 @@ void SensorTrigger::run()
136136
target_nsec = start_nsec;
137137
wait_nsec = 1e9 - now_nsec + start_nsec - 1e7;
138138
}
139-
// Keep waiting for half the remaining time until the last millisecond.
139+
// Keep waiting for half the remaining time until the last 10 milliseconds.
140140
// This is required as sleep_for tends to oversleep significantly
141141
if (wait_nsec > 1e7) {
142142
rclcpp::sleep_for(std::chrono::nanoseconds(wait_nsec / 2));
143143
}
144144
} while (wait_nsec > 1e7);
145-
// std::lock_guard<std::mutex> guard(iomutex_);
146-
// Block the last millisecond
145+
// Block the last 10 milliseconds
147146
now_nsec = rclcpp::Clock{RCL_SYSTEM_TIME}.now().nanoseconds() % (uint64_t)1e9;
148147
if (start_nsec == end_nsec) {
149148
while (now_nsec > 1e7) {

0 commit comments

Comments
 (0)