Can't get right values from SHARP sensors


I am working with the ROSbot 2.0 and am trying to implement adaptive cruise control. I already got it to drive at a constant speed, so as an intermediate step i want the ROSbot to be able to come to a full stop when driving towards a wall using data from the SHARP sensors. I came across a forum post discussing how to get data from these sensors (Getting started on SHARP GP2Y0A41SK0F) and implemented the suggested script. While in an infinite loop I tried printing the value of one of the sensors every 0.1 seconds, however it kept giving a constant value of 30, even when I tried moving things closer towards it. I was wondering if the script in the other forum post is incomplete or if there is a mistake on my end? I am not a software engineer and very new to C++ programming so any advice would be greatly appreciated. Also: I have tried this on multiple ROSbots and they all give the same results
I used the following code in my main loop.

void hMain()

    printf("\r\ndistance %f", dis1);


Kind regards,

Hello Thijs,

This is most probably due to sensor equipped on your ROSbot.
ROSbots produced since July 2018 are equipped with VL53L0X sensors instead of sharp sensors.
These are laser based sensors and have higher measuring distance.

New sensors looks like below:

For comparison, old ones are like:

Sensor measurements could be obtained with getRanges() method of class ROSbot.
You could check default firmware for code example. Initialization is in line 206 and read in 270.


Hi Lukasz,

Thanks for replying and trying to help us, I appreciate it.
I tried implementing parts of your script however the ROSbot didnt return any values. I also tried just running your entire script but this also didn’t seem to do anything. Are you sure this is the right way to get values from the distance sensors?


Hi Thijs,

Could you specify which range sensor does your ROSbot have? Is it Sharp GP2Y0A41SK0F or VL53L0X?
Depending on sensor version, ROSbot class needs to be initialized with appropriate argument.


Our ROSbot uses a VL53L0X

After a lot of trial and error I did get the sensors to work with your code!:slight_smile: However when integrated with the rest of the code, the inititialization line seems to interfere with controlling the motors in the script. Do you know if there is a reason this happens?

Line 192 causes the motors not to function like they used to.

Your motors stop working because ROSbot class is initializing motors internally.
When you call rosbot.initROSbot(), all ROSbot components are initialized, these are: motor controller, distance sensors, IMU, battery monitor and odometry. You can find code in ROSbot.cpp file in hFramework repository.

To control motors, after ROSbot initialization, you can use setSpeed() method:
rosbot.setSpeed (float linear, float angular)
Arguments are in m/s and rad/s.


1 Like