0
$\begingroup$

Rosanswers logo

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); 
  }}

Originally posted by sudo_melvinyesudas on ROS Answers with karma: 54 on 2018-01-11

Post score: 0


Original comments

Comment by jayess on 2018-01-11:
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.

Comment by jayess on 2018-01-12:
If you need a signed integer use Int16 instead of UInt16

Comment by Tom Moore on 2018-01-12:
FYI, if you need to know what's in a given message, check out its source:

http://docs.ros.org/kinetic/api/std_msgs/html/msg/Int16.html

You can see that the field you need to fill out is called 'data', as others have shown.

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

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.

Originally posted by mgruhler with karma: 12390 on 2018-01-11

This answer was ACCEPTED on the original site

Post score: 2


Original comments

Comment by sudo_melvinyesudas on 2018-01-12:
i updated my question.can you check my code ? i need signed integer input.(stepper motor angle).

Comment by mgruhler on 2018-01-12:
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...

Comment by sudo_melvinyesudas on 2018-01-12:
@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 ?

Comment by sudo_melvinyesudas on 2018-01-12:
Thank you so much. @mig

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

this works.thanx to all

$\endgroup$
0
$\begingroup$

Rosanswers logo

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

UInt16 is unsigned type of data


Originally posted by 0xd1ma with karma: 92 on 2018-01-11

This answer was NOT ACCEPTED on the original site

Post score: 1


Original comments

Comment by sudo_melvinyesudas on 2018-01-12:
i updated my question.can you check my code ? i need signed integer input.(stepper motor angle).

Comment by sudo_melvinyesudas on 2018-01-12:
thanx.it works.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.