Sensors
What are Sensors?
They are the link between your robot, and the virtual simulation. Imagine it as real sensors, that provides you with information about something.
In the case of ROSOS, Sensors are usefull for coding strategies to score goals more effectively. And what sensors are available/implemented?
- Compass Sensor: It returns an absolute angle relative to your robot's rotation
- Ball Sensor: Returns both the Angle of the ball and the distance to the ball, relative to yours robot position/orientation
- Distance Sensor: It works as a Ultrasonic Distance Sensor or Infra-red Distance sensor, reading the closest distance to something in front of it.
Finding sensors in robot
Sensors are "registered" inside every robot. You can do it yourself, but the RobotBasic
already
made that for you, and registered 6 sensors:
- 1 Compass Sensor
- 1 Ball Sensor
- 4 Distance sensors, in 4 directions: Front, Right, Back and Left.
All of them have an specific ID
, used to retrieve the Sensor from "inside" the Robot. The
method used for that is getSensor(String id)
, that returns an Sensor object.
Here is how to retrieve all the Sensors within the RobotBasic
:
Sensor ball = getSensor("BALL");
Sensor comp = getSensor("COMPASS");
Sensor usFront = getSensor("ULTRASONIC_FRONT");
Sensor usBack = getSensor("ULTRASONIC_BACK");
Sensor usLeft = getSensor("ULTRASONIC_LEFT");
Sensor usRight = getSensor("ULTRASONIC_RIGHT");
Notice that, those ID's
might change from Robot to robot, but they will be the same as long as
you use the RobotBasic
class as base for your Robot.
How to make measurements
All the sensors have the same method name, allowing your code do be agnostic about
what exactly is that sensor. The method for making a measurement and return it is the
float[] readValues()
.
This method returns an array of float
values, independent on what type of sensor it is.
Just to be more clear about it, imagine a Distance sensor. It should return a single value, containing the distance read, but that value is actually wrapped in the array.
However, there are sensors, such as the Ball sensor, that sends back to you, multiple values (one for the direction of the ball, and other for the distance), and that's the case where it is usefull.
Here is a simple example of how to make a reading:
// Save the array, then get the angle at the index 0
float[] compassValues = getSensor("COMPASS").readValues();
float currentOrientation = compassValues[0];
// Or, do it at once:
float currentOrientation = getSensor("COMPASS").readValues()[0];
// Or even save the Sensor before reading,
// to avoid "getting" the sensor multiple times
Sensor sensor = getSensor("COMPASS");
float currentOrientation = sensor.readValues()[0];
However, there is another method to help you with that, called float readValue(int index)
,
that does the same as reading the array of values and then getting that Specific index:
// Before:
float someValue = sensor.readValues()[0];
// After:
float someValue = sensor.readValue(0);
// Reading the Ball angle/distance:
float ballAngle = getSensor("BALL").readValue(0);
float ballDist = getSensor("BALL").readValue(1);
Specific documentation for each robot is available on the Menu.
Noise and Imprecisions
To be as closer to reality as possible, we have added some "noise" to the readings. The ammount of noise can vary from sensor to sensor, and we set the default values to something that is close to what happens with generic physical sensors.