1
+ #include < ros/ros.h>
2
+
3
+ #include " mraa/common.hpp"
4
+ #include " mraa/gpio.hpp"
5
+
6
+ #include < upboard_ros/Gpio.h>
7
+ #include < upboard_ros/ListGpio.h>
8
+
9
+ #include < vector>
10
+
11
+ class pGpio : public mraa ::Gpio{
12
+ private:
13
+ int _pin;
14
+
15
+ public:
16
+ pGpio (int pin, bool owner=true , bool raw=false ):mraa::Gpio(pin,owner,raw){
17
+ _pin=pin;
18
+ }
19
+ int getPin (){return _pin;}
20
+ ~pGpio () {};
21
+ };
22
+
23
+ std::vector<pGpio*> listgpio_output;
24
+
25
+
26
+ void setGpio (uint8_t pin, uint8_t value){
27
+ int k=0 ;
28
+ bool found=false ;
29
+ while ((!found) && (k<listgpio_output.size ())){
30
+ if (listgpio_output[k]->getPin ()==pin){
31
+ found=true ;
32
+ try {
33
+ listgpio_output[k]->write (value);
34
+ }
35
+ catch (std::exception & e){
36
+ ROS_ERROR (" %s" ,e.what ());
37
+ }
38
+ }
39
+ k++;
40
+ }
41
+ if (!found){
42
+ ROS_WARN (" GPIO %d is not defined. Check your configs" ,pin);
43
+ }
44
+ }
45
+
46
+
47
+ void gpioCallback (const upboard_ros::ListGpio & msg){
48
+ for (int i=0 ; i<msg.gpio .size (); i++){
49
+ setGpio (msg.gpio [i].pin ,msg.gpio [i].value );
50
+ }
51
+ }
52
+
53
+ int main (int argc, char **argv){
54
+ ros::init (argc, argv, " upboard_ros/gpio_node" );
55
+ ros::NodeHandle nh;
56
+
57
+ /* rostopic pub /upboard/gpio/write upboard_ros/ListGpio "{header: auto, gpio:[{pin: 22, value: 0}]}" --once */
58
+
59
+ std::vector<int > paramlist;
60
+
61
+ // load digital output pins
62
+ if (nh.getParam (" upboard_ros/DigitalOut" ,paramlist)){
63
+ for (int i=0 ; i<paramlist.size (); i++){
64
+ try {
65
+ pGpio* tmp = new pGpio (paramlist[i]);
66
+ tmp->dir (mraa::DIR_OUT);
67
+ listgpio_output.push_back (tmp);
68
+
69
+ ROS_INFO (" Pin %d is configured as Digital Output" ,paramlist[i]);
70
+ }
71
+ catch (std::exception & e){
72
+ ROS_ERROR (" %s" ,e.what ());
73
+ }
74
+ }
75
+ }
76
+
77
+
78
+ ros::Subscriber gpiosub = nh.subscribe (" /upboard/gpio/write" , 10 , gpioCallback);
79
+
80
+ ros::spin ();
81
+
82
+
83
+ // put all outputs to 0
84
+ for (int i=0 ; i<listgpio_output.size (); i++){
85
+ try {
86
+ listgpio_output[i]->write (0 );
87
+ }
88
+ catch (std::exception & e){
89
+ ROS_ERROR (" %s" ,e.what ());
90
+ }
91
+ }
92
+
93
+ return 0 ;
94
+ }
0 commit comments