Robotics StackExchange | Archived questions

Can I use WiringPi on ROS indigo ?

Please teach me how to use by ROS indigo.

I can use WiringPi. I can use this program.

#include < iostream >
#include < wiringPi.h >
#include < conio.h >

int main()
{
  if (wiringPiSetupGpio() == \-1) {
    std::cout << "cannot setup gpio." << std::endl;
    return 1;
  }

  pinMode(18, PWM_OUTPUT);
  pinMode(19, PWM_OUTPUT);
  pwmSetMode(PWM_MODE_MS);
  pwmSetClock(266);
  pwmSetRange(1024);
  pwmWrite(18,110);
  pwmWrite(19,105);

  while (1) {
    pwmWrite(18,110);
    if (kbhit() && getch() == 'w'){
      while(1){
      pwmWrite(18,117); //前進
      if(kbhit() && getch() == 'x') break;
       }
    }
    if (kbhit() && getch() == 's'){
      while(1){
      pwmWrite(18,100);//後退
      if(kbhit() && getch() == 'x') break;
       }
    }
    if (kbhit() && getch() == 'd'){
      pwmWrite(19,90);//右旋回
    }
    if (kbhit() && getch() == 'a'){
      pwmWrite(19,120);//左旋回
    }
    if (kbhit() && getch() == 'e'){
      pwmWrite(19,105); //直進
     }
    if (kbhit() && getch() == 'q'){ //停止
    pwmWrite(18,110);
    pwmWrite(19,105);
    break;
    }
  }
  pwmWrite(18,110);
  pwmWrite(19,105);
  return 0;
} 

and I want to control. It is recieved joystick data to control GPIO.

Asked by Kazuki Kadonaka on 2015-11-17 00:22:25 UTC

Comments

Hi, have you been able to get it working together?

Asked by wilsonz91 on 2015-12-04 04:17:59 UTC

Thanks your comment.Now studying.

Asked by Kazuki Kadonaka on 2015-12-08 00:49:21 UTC

Answers

Look at the tutorials at http://wiki.ros.org/ROS/Tutorials

They show you how to set up a workspace to create ROS compatible code, and then publish or subscribe to messages in ROS. You would need to incorporate the code for subscribing into your code for wiringPi, assuming you want to control the pins based on messages received from a ROS topic

Asked by nickw on 2015-12-05 05:19:26 UTC

Comments

Thanks your comment.Now studying.

Asked by Kazuki Kadonaka on 2015-12-08 00:49:43 UTC

I have manage to get wiringPi and ROS working together. First step, you need to include in the CMakeList.txt where to find the lirary, for me it was:

FIND_LIBRARY(WIRINGPI_LIBRARY wiringPi /home/odroid/wiringPi)

The first parameter WIRINGPI_LIBRARY is just a name you give it which you will then use to refer within CMakeList. Second parameter is the library name and third parameter is the location on your system.

To use it, It depends on whether you're working using Python or C++.

1) For C++, include the library in your code and it would be something like this:

#include “wiringPi.h”       //include the library

………

wiringPiSetup();            //library setup function
pinMode(11,OUTPUT);         //set pin GPX2.1 as output. Based on wiringPi
//GPIO library map 

………

digitalWrite(11,data);      //write data(binary) to pin

And then in your CMakeList.txt, add the following:

add_executable(slave src/slave.cpp)
target_link_libraries(slave ${CATKIN_LIBRARIES} ${WIRINGPI_LIBRARY}
add_dependencies(slave beginner_tutorials_generate_messages_cpp wiringPi)

2) For python, I THINK its simply including the library in your code (not tested)

Asked by wilsonz91 on 2015-12-08 02:56:43 UTC

Comments

Thanks,please check.

Asked by Kazuki Kadonaka on 2015-12-10 03:59:31 UTC

This was very helpful; but be sure to use the direct path to your wiringPi folder you cloned in the FIND_LIBRARY command in order for full functionality to actually work.

Asked by luc on 2016-03-20 15:59:05 UTC

Hi, I followed your instruction and setup the wiringPi in my ROS node, but I encountered the issue that "wiringPiSetup: Must be root. (Did you forget sudo?)" I know that this is related to the root permission, how do you overcome this? Thanks in advance.

Asked by DKWatson on 2016-06-16 00:27:54 UTC

It's been a while since I used it but I'm guessing that happens when you try to run your code? If that's the case, then you have to execute your code as SU i.e. in the terminal have SU rights, source the ROS bash file then run your code. Let me know if it works.

Asked by wilsonz91 on 2016-06-16 05:19:23 UTC

Yeah, when I did it, I had to do su root, enter my password, then it worked.

Asked by luc on 2016-06-16 12:21:37 UTC

If you need any reference for using wiring_pi with ROS, you can check out my project here: https://github.com/lucbettaieb/origami_bot

Asked by luc on 2016-06-16 12:23:28 UTC

Finally get it down! @wilsonz91, @luc, Thanks so much for replying me the comment. They really help. Thank you @luc for sharing your project code to me, thanks you a Tons!

Asked by DKWatson on 2016-06-16 14:52:41 UTC

BTW, to whom may concern, su root doesn't work for me. But sudo -i will just did the same job.

Asked by DKWatson on 2016-06-16 15:16:48 UTC

I had exactly the same problem and I have been struggling with it for a long time and now it's solved! Thanks a lot!

Asked by irmakguzey on 2018-03-02 15:30:17 UTC

Please check

#include <ros/ros.h>
#include <sensor_msgs/Joy.h>
#include <geometry_msgs/Twist.h>
#include <wiringPi.h> 


class JoyTwist
{
public:
  JoyTwist()
  {
    ros::NodeHandle node;
    joy_sub_ = node.subscribe("joy", 1, &JoyTwist::joyCallback, this);
  }

  void joyCallback(const sensor_msgs::Joy &joy_msg)
  {
    wiringPiSetup(); 
    pinMode(18,OUTPUT); 
    pinMode(19,OUTPUT);
    if (joy_msg.buttons[0] == 1)
    {
      digitalWrite(18, 110 + joy_msg.axes[1] * 10);
      digitalWrite(19, 105 + joy_msg.axes[0] * 15);         
    }
  }
private:
  ros::Subscriber joy_sub_;
};

int main(int argc, char **argv) {
  ros::init(argc, argv, "joy_twist");
  JoyTwist joy_twist;
  ros::spin();
}

Asked by Kazuki Kadonaka on 2015-12-10 04:00:46 UTC

Comments