Advertisement
Guest User

ard

a guest
May 12th, 2012
516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. #include <ros.h>
  2. #include <std_msgs/String.h>
  3.  
  4.  
  5.  
  6. #include "WString.h"
  7. #include <std_msgs/Empty.h>
  8. #include <Servo.h>
  9. ros::NodeHandle  nh;
  10.  
  11. double left_signal = 0, right_signal = 0;
  12. void messageCb( const std_msgs::String &msg){
  13.       Serial.println("in message cb");
  14.       sscanf(msg.data, "%lf %lf", &left_signal, &right_signal);
  15.       Serial.println(left_signal);
  16.       Serial.println(right_signal);
  17.      
  18. }
  19.  
  20. ros::Subscriber<std_msgs::String> sub("/game_controller", &messageCb );
  21.  
  22. Servo myservo_right;  // create servo object to control a servo
  23. Servo myservo_left; //left side motor control
  24.  
  25.  
  26. int potpin = 0;  // analog pin used to connect the potentiometer
  27. int val;    // variable to read the value from the analog pin
  28.  
  29. void setup()
  30. {
  31.  
  32.   Serial.begin(9600);
  33.   myservo_right.attach(5);  // attaches the servo on pin 9 to the servo object
  34.   myservo_left.attach(6);
  35.   nh.initNode();
  36.   nh.subscribe(sub);
  37. }
  38.  
  39. void loop()
  40. {
  41.   //val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  42.   //val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
  43.   myservo_right.write(right_signal);  // sets the servo position according to the scaled value
  44.   myservo_left.write(left_signal);
  45.   nh.spinOnce();
  46.   delay(15);    
  47.   // waits for the servo to get there
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement