ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Keyboard Teleop settings.

asked 2018-03-08 01:43:04 -0500

updated 2021-04-24 02:47:20 -0500

miura gravatar image

I write a program in arduino. i want to do the same with ros-keyboard_teleop. can sombody guide me ?any tutorial link.

void setup() 
{
  pinMode(24, OUTPUT);
  pinMode(25, OUTPUT);
  Serial.begin(9600);
}


void loop() 
{ 
  if (Serial.available())
  {
    char data = Serial.read();

    if(data == 'i')
    {
      digitalWrite(24, LOW);
      digitalWrite(25, HIGH);
     Serial.println("reverse");
    }
    else if(data == 'j')
    {
      digitalWrite(24, HIGH);
      digitalWrite(25, LOW);
      Serial.println("forward");

    }
   else
    {
      digitalWrite(24,LOW);
      digitalWrite(25,LOW);
     Serial.println("stop"); 
    }}}

i did a program. but i think there is a problem with

else if (y = vel.linear.y - 1.0) can you check my code and tell me where is the problem?

   #include <ros.h>
    #include <geometry_msgs/Twist.h>
    float x;
    float y; 
    ros::NodeHandle nh;

    void velCallback(  const geometry_msgs::Twist& vel)
    {
       if (x = vel.linear.x - 1.0) //Forward move.
          {
          digitalWrite(24, LOW);
          digitalWrite(23, HIGH);
          }
    else if (y = vel.linear.y - 1.0)   //Backward move
    {
          digitalWrite(24, HIGH);
          digitalWrite(23, LOW);
    }
    else 
    {
        digitalWrite(24, LOW);     //STOP
          digitalWrite(23, LOW);
    }
        }

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

void setup() 
{
     nh.initNode();
     nh.subscribe(sub);
}

void loop() 
{
     nh.spinOnce();
     delay(10);
}
edit retag flag offensive close merge delete

Comments

teleop_twist_keyboard publishes a geometry_msgs/Twist message type on the /cmd_vel topic. You should make your arduino subscribe to this. A few other projects that do this are https://sungjik.wordpress.com/tag/ros/ and https://answers.ros.org/question/2585...

ahendrix gravatar image ahendrix  ( 2018-03-08 02:30:27 -0500 )edit

can you please write how the code should be ? i checked the links.but still dont have idea how to do so.

sudo_melvinyesudas gravatar image sudo_melvinyesudas  ( 2018-03-08 02:49:26 -0500 )edit

The code in https://answers.ros.org/question/2585... is how most of the code should be. The only thing you need to add is the digitalWrite commands for your motors in velCallback.

ahendrix gravatar image ahendrix  ( 2018-03-08 11:01:14 -0500 )edit

i just updated my program.can you check the code ?i face the problem with y axis.

rosrun teleop_twist_keyboard teleop_twist_keyboard.py

is this the twist_keboard command

sudo_melvinyesudas gravatar image sudo_melvinyesudas  ( 2018-03-09 00:22:13 -0500 )edit

Why do you use Y axis for a backward move ? Y axis is usually for lateral moves, and Z axis of vel.angular for turning.

Can you explain in the question how your robot moves ? (2 motorized wheels maybe) I think it would help people to understand your needs.

simchanu gravatar image simchanu  ( 2018-03-09 11:17:33 -0500 )edit

sorry im new to this.i dont know how the code supposed to be.what i want is if i press "i" it should execute first IF condition which is forward move.and if i press any other key whether it is j or k any, it goes backward.

sudo_melvinyesudas gravatar image sudo_melvinyesudas  ( 2018-03-09 23:22:00 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2018-03-09 23:17:06 -0500

ahendrix gravatar image

The conditionals in your velocity callback aren't meaningful c++:

if (x = vel.linear.x - 1.0) //Forward move.
{
      digitalWrite(24, LOW);
      digitalWrite(23, HIGH);
}
else if (y = vel.linear.y - 1.0)   //Backward move
{
      digitalWrite(24, HIGH);
      digitalWrite(23, LOW);
}
else 
{
      digitalWrite(24, LOW);     //STOP
      digitalWrite(23, LOW);
}

Since you use the single equals sign you're changing the value of x and y instead of checking it. Instead you should use the less-than and greater-than operators (and a little bit of wiggle room so you don't need to send exactly 0 to stop):

if (vel.linear.x > 0.1) //Forward move.
{
      digitalWrite(24, LOW);
      digitalWrite(23, HIGH);
}
else if (vel.linear.x < -0.1)   //Backward move
{
      digitalWrite(24, HIGH);
      digitalWrite(23, LOW);
}
else 
{
      digitalWrite(24, LOW);     //STOP
      digitalWrite(23, LOW);
}
edit flag offensive delete link more

Comments

is there any other function than this vel.linear.x what if i want to change the y or z axis ? keyword for that ?

sudo_melvinyesudas gravatar image sudo_melvinyesudas  ( 2018-03-09 23:33:33 -0500 )edit

Yes, the twist message also has y and z components, as well as x, y and z angular components. The full documentation is on the geometry_msgs page on the ROS wiki.

ahendrix gravatar image ahendrix  ( 2018-03-10 12:06:34 -0500 )edit

teleop_twist_keyboard is able to send x, y and z velocities and yaw (rotation about Z), but from your code it seems like your robot can only move forwards and backwards (+/- X) so I'm not sure that the other axes will be useful for you.

ahendrix gravatar image ahendrix  ( 2018-03-10 12:08:09 -0500 )edit
0

answered 2018-03-09 01:04:25 -0500

if you use turtlesim to simluate the robot , I sure the robot has no Y freedom , so vel.linear.y is always 0

edit flag offensive delete link more

Comments

so what should i do ? i just want if i press "i"it go forward,if i press "j" or any other key it should go bacckward.can you help me to do the code ?

sudo_melvinyesudas gravatar image sudo_melvinyesudas  ( 2018-03-09 01:43:30 -0500 )edit

you better refer the source code from here

build the code , then you will get a ros node, run the node from console with rosrun ... you can capture the keyborad input , and you can control the robot

lsw gravatar image lsw  ( 2018-03-09 03:42:05 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2018-03-08 01:43:04 -0500

Seen: 2,256 times

Last updated: Mar 09 '18