[solved]:-How to publish negative values from command line ?
When i try this command.i got an error like this.How can i publish negative values from command line ?
rostopic pub servo std_msgs/UInt16 -180
Usage: rostopic pub /topic type [args...]
rostopic: error: no such option: -8
this is my code:-
#include <ros.h>
#include <std_msgs/Empty.h>
#include <std_msgs/UInt16.h>
signed int angle;
const int stepPin = 9;
const int dirPin = 8;
int x;
ros::NodeHandle nh;
void pwm( const std_msgs::UInt16& cmd_msg)
{
angle=cmd_msg.data;
//angle=map(angle,280,1720,-720,720);
if (angle<0)
{
cw();
delay(100);
}
else
{
ccw();
delay(100);
}
}
ros::Subscriber<std_msgs::UInt16> sub("servo", pwm);
void setup(){
// pinMode(angle, OUTPUT);
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
nh.initNode();
nh.subscribe(sub);
// servo.attach(9); //attach it to pin 9
}
void loop()
{
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);
}}
The answers are correct saying that a negative value for a
Uint
is wrong but they don't give much explanation. Here's why: theU
inUint
is for unsigned meaning not negative.Uint
s by definition cannot be negative.If you need a signed integer use
Int16
instead ofUInt16
FYI, if you need to know what's in a given message, check out its source:
http://docs.ros.org/kinetic/api/std_m...
You can see that the field you need to fill out is called 'data', as others have shown.