How to take the average of two V5 Rotation Sensors using Vexcode Pro V5

Hello. I hope everyone’s season of Vex Robotics is going well. We are currently working on a new robot, and I am trying to take the average of two V5 Rotational Sensors. The sensors are mounted to non-powered wheels on the chassis (one sensor is mounted on the far left and the other is mounted on the far right). They will be used to make a more accurate autonomous because we will be taking the reading of the ‘distance’ directly from a wheel rather than from the motor. When measuring the ‘distance’ from the wheel to the motor, the slop in the gears, wheels, bearings, etc., will cause the autonomous to be less accurate. With this, I have decided to use double as my value because it can be positive or negative with decimals. I’m getting an error that says “no viable conversion from ‘vex::rotation’ to ‘double’” I truly don’t know what is wrong or how to fix this. Below the values is where I am attempting to add the value together and divide them by 2 for an average. This average then would be used in the PID code. If anyone has any ideas on how to fix this issue and is willing to share the knowledge, that would be greatly appreciated. Good luck in Tipping Point.

Left_OD= Left Chassis Rotation Sensor
Right_OD= Right Chassis Rotation Sensor

Code:

First all calculations using sensors need to be inside a function not in the global scope.

next you would read position from the rotation sensor using the “position()” member function, for example.

// returns average position in degrees
double
get_average() {
  double left_position  = Left_OD.position( rotationUnits::deg );
  double right_position = Right_OD.position( rotationUnits::deg );
  double average_position = (left_position + right_position ) / 2;
  
  return average_position;
}
7 Likes

Ok. Now I am trying to put it into a while statement. Would i just put something along the lines of the code below? Sorry, this is troublesome, I have never created code where it gets the average of two sensors.

image

Why would you think that would work ? Where did “average_position” come from.

You created the function called “get_average”, so call it like a normal function,

while( get_average() < 1 ) {
}

but it’s very unlikely to ever be less than one, you are calculating average, not absolute difference or anything like that.

4 Likes

I got the “average position” from inside “get average”. It makes since now why it wasn’t working. The whole thing has to be used, not just one part. It works great. Thank you so much.

1 Like