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

cant assign the value to 'cmd_msg.data' in runtime.

asked 2018-01-10 00:49:34 -0600

updated 2018-01-10 05:51:46 -0600

I am doing a stepper motor programming.what i want is "when i enter an angle on command line,then stepper should rotate that angle". but i cant assign the value in runtime. help me please.

#include <ros.h>
#include <std_msgs/Empty.h>
#include <std_msgs/UInt16.h>
signed int angle;
int enable2 = 13;
const int stepPin = 9; 
const int dirPin = 8;
int x;

ros::NodeHandle  nh;
void pwm( const std_msgs::UInt16& cmd_msg)
{

 analogWrite(enable2, cmd_msg.data);
angle=analogRead(enable2);


}


ros::Subscriber<std_msgs::UInt16> sub("servo", pwm);

void setup(){
 // pinMode(angle, OUTPUT); 
 pinMode(enable2, OUTPUT);   
 pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);
  nh.initNode();
  nh.subscribe(sub);

 // servo.attach(9); //attach it to pin 9
}

void loop()
{

   if (angle<0)
     {
       cw();
       delay(100);
      }
 else
   {
    ccw();
    delay(100);
   }
delay(10000);
nh.spinOnce();
  delay(1);
}
void cw()
{
  digitalWrite(dirPin,LOW); 
  for(x = 0; x < 17.77*(angle*-1); x++) {
    digitalWrite(stepPin,HIGH); 
    delayMicroseconds(500); 
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(500); 
  }}
  void ccw()
{
  digitalWrite(dirPin,HIGH); 
  for(x = 0; x < 17.77*angle; x++) {
    digitalWrite(stepPin,HIGH); 
    delayMicroseconds(500); 
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(500); 
  }}
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2018-01-10 05:07:34 -0600

gvdhoorn is right. You are setting up a subscriber with a callback function called cmd_msg, but cmd_msg has not been defined, further more you seem to have coded your callback function into the loop function so it would run continuously.

You'll need to define a function called cmd_msg and access the angle that the message contains as shown below:

void cmd_msg(const std_msgs::UInt16::ConstPtr& msg)
{
  int angle = msg.data;
  ...
  ...
}

Then add the rest of the code you have inside loop into this callback function.

Have a look at the publisher and subscriber example for some pointers.

Hope this helps.

edit flag offensive delete link more

Comments

Note that the OP is using rosserial. The tutorial may help, but could also confuse.

gvdhoorn gravatar image gvdhoorn  ( 2018-01-10 05:31:57 -0600 )edit

brothers, can you check the question once more ? i just edited my code.thats why.

and now the problem is i csnt assign the value in runtime . and it shows lost the sync with device.

sudo_melvinyesudas gravatar image sudo_melvinyesudas  ( 2018-01-10 05:48:25 -0600 )edit
2

I'm not your brother, but:

void loop(){
  ..
  delay(10000);
}

you cannot delay the loop() for 10 seconds like that.

gvdhoorn gravatar image gvdhoorn  ( 2018-01-10 05:53:08 -0600 )edit

ok sorry. but still it wont make any difference.

analogWrite(enable2, cmd_msg.data);
    angle=analogRead(enable2);

is it right ?thus can i pass the value which i enter in cammandline to variable angle ?

sudo_melvinyesudas gravatar image sudo_melvinyesudas  ( 2018-01-10 06:20:39 -0600 )edit
0

answered 2018-01-10 02:14:47 -0600

gvdhoorn gravatar image

updated 2018-01-10 02:16:00 -0600

This does not appear to be a ROS problem, but a C/C++ programming one.

So where do you declare / define cmd_msg?

ros::Subscriber<std_msgs::UInt16> sub("servo", &cmd_msg);

According to this, cmd_msg is a function.

angle=cmd_msg.data;

According to this, it should be some kind of structure or object.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-01-10 00:49:34 -0600

Seen: 330 times

Last updated: Jan 10 '18