ROS-SERIAL and Arduino Motor Control for Object_tracker

1,316 views
Skip to first unread message

Gani Siva kumar

unread,
Oct 22, 2015, 2:17:44 AM10/22/15
to ros-by-example
Hello everyone,I had written a code in Arduino to drive motors for Object Tracker code form ROS-BY EXAMPLE book.Code is compiled without any errors but i am not getting the robot movements according to tracker.I am using Arduino Mega and hydro version of ROS. Please help me with mistakes in code or give me a code which will move the robot correctly.Thanks in advance.

#include <ArduinoHardware.h>
#include <ros.h>
#include <geometry_msgs/Twist.h>

#define LEFT_MOTOR_1   48
#define LEFT_MOTOR_2   49
#define LEFT_MOTOR_PWM 6
#define RIGHT_MOTOR_PWM 5
#define RIGHT_MOTOR_1   46
#define RIGHT_MOTOR_2   47

ros::NodeHandle nh;
int lowSpeed = 220;
int highSpeed = 30;

void messageCb( const geometry_msgs::Twist& msg){
  
   if(msg.angular.z>0)
   {
      moveLeft();
   }
   else if(msg.angular.z<0)
   {
      moveRight();
   }
   else if(msg.angular.z == 0)
   {
      moveFront();
   }
}

ros::Subscriber<geometry_msgs::Twist> sub("cmd_vel", &messageCb );



void setup()
{
  pinMode(LEFT_MOTOR_1, OUTPUT);
  pinMode(LEFT_MOTOR_2, OUTPUT);
  pinMode(LEFT_MOTOR_PWM, OUTPUT);
  pinMode(RIGHT_MOTOR_PWM, OUTPUT);
  pinMode(RIGHT_MOTOR_1, OUTPUT);
  pinMode(RIGHT_MOTOR_2, OUTPUT);

 //initialization of motors

  digitalWrite(RIGHT_MOTOR_1, LOW);
  digitalWrite(RIGHT_MOTOR_2, LOW);
  digitalWrite(RIGHT_MOTOR_1, LOW);
  digitalWrite(RIGHT_MOTOR_2, LOW);
  
  analogWrite(RIGHT_MOTOR_PWM, lowSpeed);
  analogWrite(RIGHT_MOTOR_PWM, lowSpeed); 
  nh.initNode();
  nh.subscribe(sub);

void loop()
{
 nh.spinOnce();
 delay(20);
 }
void moveLeft(){
  
  digitalWrite(LEFT_MOTOR_1, LOW);
  digitalWrite(LEFT_MOTOR_2, HIGH);
  digitalWrite(RIGHT_MOTOR_1, LOW);
  digitalWrite(RIGHT_MOTOR_2, HIGH);
  
  analogWrite(LEFT_MOTOR_PWM, lowSpeed);
  analogWrite(RIGHT_MOTOR_PWM, highSpeed);  
  
  delay(1500);
  moveFront();
  
}

void moveFront(){
  
  digitalWrite(LEFT_MOTOR_1, LOW);
  digitalWrite(LEFT_MOTOR_2, HIGH);
  digitalWrite(RIGHT_MOTOR_1, LOW);
  digitalWrite(RIGHT_MOTOR_2, HIGH);
  
  analogWrite(LEFT_MOTOR_PWM, highSpeed);
  analogWrite(RIGHT_MOTOR_PWM, highSpeed);
  
}

void moveRight(){
  
  digitalWrite(LEFT_MOTOR_1, LOW);
  digitalWrite(LEFT_MOTOR_2, HIGH);
  digitalWrite(RIGHT_MOTOR_1, LOW);
  digitalWrite(RIGHT_MOTOR_2, HIGH);
  
  analogWrite(LEFT_MOTOR_PWM, highSpeed);
  analogWrite(RIGHT_MOTOR_PWM, lowSpeed);
  
  delay(1500);
  moveFront();
  
}


P Tiago Pereira

unread,
Oct 23, 2015, 9:01:06 AM10/23/15
to ros-by-example
Gani,

I don't see any major issue with your arduino code, must be an issue related to rosserial (server/node) side.

As far I recall the books don't cover any ROSSERIAL, i used before rosserial and after a few tests i  found major limitations.

The best solution is to use the ros arduino bridge mentioned in the book, or write your own serial bridge.       

Tiago

Patrick Goebel

unread,
Oct 23, 2015, 10:05:02 AM10/23/15
to ros-by-...@googlegroups.com
Hi Gani,

Like Tiago, I don't see any problems with your code but I haven't tried rosserial in several years.  As with any program that is not working the way you expect, I suggest placing some debug statements here and there so you can verify that the Twist messages are actually being received and your functions are actually being called (e.g. moveLeft(), moveFront(), etc.)

--patrick
--
You received this message because you are subscribed to the Google Groups "ros-by-example" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ros-by-exampl...@googlegroups.com.
Visit this group at http://groups.google.com/group/ros-by-example.
For more options, visit https://groups.google.com/d/optout.

Gani Siva kumar

unread,
Oct 23, 2015, 11:44:16 PM10/23/15
to ros-by-example
Hi Tiago and Patrick,

Thanks for the insights.
Reply all
Reply to author
Forward
0 new messages