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

Reading Potentiometer in ROS

asked 2016-11-13 00:02:17 -0500

marwa eldiwiny gravatar image

updated 2016-11-13 02:39:08 -0500

NEngelhard gravatar image

Hey Folks,

I am trying to read potentiometer value in ROS, but I stuck with error

#include <ros.h>
#include <ros/time.h>

/*
  AnalogReadSerial
  Reads an analog input on pin 0, prints the result to the serial monitor.
  Graphical representation is available using serial plotter (Tools > Serial Plotter menu)
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

  This example code is in the public domain.
*/

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(1);        // delay in between reads for stability
}

To be honest, sometimes I don't understand why baudrate is not mentioned in ROSlib. or what the right code I should write for receiving analog signal.

Best, Marwa

edit retag flag offensive close merge delete

Comments

You forgot to actually describe your problem. What happens? What should happen? Which error message do you get?

NEngelhard gravatar image NEngelhard  ( 2016-11-13 02:40:41 -0500 )edit

Also: even though ros.h is included in the code snippet, nothing of rosserial is used here, so this code cannot communicate with the PC side of rosserial at all. rosserial acts as a bridge, it isn't a generic connect-whatever-to-a-serial-port-and-it-will-work library.

gvdhoorn gravatar image gvdhoorn  ( 2016-11-13 04:05:51 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2016-11-13 03:10:03 -0500

marcoresk gravatar image

updated 2016-11-13 03:10:35 -0500

I always get an error (especially unsync errors) when I use Serial.something instructions inside Arduino (your code looks like arduino even you didn't mention it) together with ROS communication. If you use rosserial to communicate with arduino, you have to use nothing but a ROS publisher. But I admit I don't know why.

I mainly followed these tutorial and remove any other Serial.something instructions from arduino code. Changing a bit some of these tutorials I manage to publish in ROS topics: numbers, arrays of numbers, strings. I have some problems with a string subscriber.

edit flag offensive delete link more

Comments

If you use rosserial to communicate with arduino, you have to use nothing but a ROS publisher. But I admit I don't know why.

that's relatively simple: both Serial.* and ROS will use the serial port to communicate with the other side. The rosserial node(s) running on your PC will not ..

gvdhoorn gravatar image gvdhoorn  ( 2016-11-13 04:01:36 -0500 )edit

.. deal with anything but the binary protocol used by the Arduino side of rosserial, so if you use anything from Serial, there will be rosserial traffic mixed with other "stuff". rosserial has no way to filter that out, so it cannot deal with that. Only thing it can conclude is that ..

gvdhoorn gravatar image gvdhoorn  ( 2016-11-13 04:03:03 -0500 )edit

.. some of the bytes it is expecting have been corrupted, leading to a sync error.

It is possible to use Serial.* and rosserial together, but they would have to be configured to use different serial ports.

gvdhoorn gravatar image gvdhoorn  ( 2016-11-13 04:04:01 -0500 )edit

Thanks, simply I am trying read potentiometer values in ROS and plotting them in python plot tool, I am doing this because I don't want to use MATLAB.

I am using arduino uno, potentiometer that is it.

marwa eldiwiny gravatar image marwa eldiwiny  ( 2016-11-14 01:23:20 -0500 )edit

So in conclusion did you use rosserial or not? Can you update if you have changed anything?

Taylorson gravatar image Taylorson  ( 2018-11-20 00:43:33 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2016-11-13 00:02:17 -0500

Seen: 800 times

Last updated: Nov 14 '16