Ask Your Question
0

[solved]:-How to publish negative values from command line ?

asked 2018-01-11 04:33:10 -0500

updated 2018-01-12 01:43:04 -0500

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); 
  }}
edit retag flag offensive close merge delete

Comments

The answers are correct saying that a negative value for a Uint is wrong but they don't give much explanation. Here's why: the U in Uint is for unsigned meaning not negative. Uints by definition cannot be negative.

jayess gravatar imagejayess ( 2018-01-11 10:23:30 -0500 )edit

If you need a signed integer use Int16 instead of UInt16

jayess gravatar imagejayess ( 2018-01-12 01:25:35 -0500 )edit

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.

Tom Moore gravatar imageTom Moore ( 2018-01-12 04:22:46 -0500 )edit

3 Answers

Sort by ยป oldest newest most voted
2

answered 2018-01-11 04:54:51 -0500

mgruhler gravatar image

Two issues:

  1. The command should be: rostopic pub servo std_msgs/UInt16 "data: 180". You have to specify what the fields of the message have. The -180 is intrepreted as command line Options (and rightly so). Just hit Tab after you add the type, and the fields appear automatically.
  2. Putting a negative value in an unsigned type is just wrong and will throw another error.
edit flag offensive delete link more

Comments

i updated my question.can you check my code ? i need signed integer input.(stepper motor angle).

sudo_melvinyesudas gravatar imagesudo_melvinyesudas ( 2018-01-11 23:30:09 -0500 )edit
1

Well, I'm not quite sure what your question is. If you want to use signed data types (i.e. negative values as well), just replace every occurence of UInt16 with Int16.

If you don't know the difference, maybe read around a bit on Google about signed/unsigned types...

mgruhler gravatar imagemgruhler ( 2018-01-12 00:53:38 -0500 )edit

@mig i replace UInt16 with Int16. and try this

rostopic pub servo std_msgs/Int16 -360
Usage: rostopic pub /topic type [args...]

rostopic: error: no such option: -3

How to publish Negative values in Int16 ?. cammand for that ?

sudo_melvinyesudas gravatar imagesudo_melvinyesudas ( 2018-01-12 01:38:00 -0500 )edit

Thank you so much. @mig

rostopic pub servo std_msgs/Int16 "data: -720"

this works.thanx to all

sudo_melvinyesudas gravatar imagesudo_melvinyesudas ( 2018-01-12 01:42:14 -0500 )edit
1

answered 2018-01-11 04:50:55 -0500

updated 2018-01-11 04:51:53 -0500

use TAB for autocomplete rostopic pub /servo std_msgs/UInt16 "data: 0"

UInt16 is unsigned type of data

edit flag offensive delete link more

Comments

i updated my question.can you check my code ? i need signed integer input.(stepper motor angle).

sudo_melvinyesudas gravatar imagesudo_melvinyesudas ( 2018-01-11 23:30:04 -0500 )edit

thanx.it works.

sudo_melvinyesudas gravatar imagesudo_melvinyesudas ( 2018-01-12 01:42:38 -0500 )edit
0

answered 2018-01-12 01:55:32 -0500

jayess gravatar image

updated 2018-01-12 01:56:53 -0500

There were actually two issues with what you were trying

  1. You were trying to publish a negative UInt16
  2. rostopic pub saw the negative value as command line option

Solution:

  1. Like everyone said, negative UInt16 values makes no sense. Unsigned numbers cannot be negative. Please read up on them.
  2. Using a - when using the command line usually signals that you're using a flag (option). As the error

    rostopic: error: no such option: -8
    

    tells you, it thinks that you're trying to use 8 as an option (which isn't what you want). You can either publish it like you said in the comment to @mig 's answer or use

    rostopic pub servo std_msgs/Int16 -- -8
    

Note: in the future, you can use Google to help you find the answer by entering the following as your search term:

rostopic pub no such option site:answers.ros.org

which will limit your search to Google's index of ROS Answers. Next time, just replace rostopic pub no such option with whatever error you have. This time the first result would have led you to the solution for #2: #q61598

edit flag offensive delete link more

Comments

thanx.its worked

sudo_melvinyesudas gravatar imagesudo_melvinyesudas ( 2018-01-12 02:26:04 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2018-01-11 04:33:10 -0500

Seen: 422 times

Last updated: Jan 12 '18