Python API for intertial sensor...how to reset heading or rotation

there is a resetHeading or resetRotation method in the C++ API…is there an equivalent in the Python API or do I just use setRotation(0, DEGREES)? This doesn’t seem to be working but there may be other issues…I’m still debugging but wanted to see if anyone had encountered this…thank you

yes, reset_rotation() and reset_heading() are both available, remember all Python functions use snake case style. I recommend you use the VSCode extension which has improved intellisense for Python functions.

4 Likes

James,

Thank you. I tried this and in Python it is not working. I wrote a test program in Python with a loop of ten iterations to: turn the robot 45 degrees, reset rotation and then print the current rotation value. It works perfectly in C++, all printed values after reset are 0, in Python it’s for the most yielding an increasing rotation value. I have an autonomous program in Python and am running into this problem.

post your code here, I just tested an IMU on V5, seems ok.

MicroPython v1.13-312-ga57027e09-dirty on 2022-08-10; Vex V5 with Zynq (v0.2)
Type "help()" for more information.
>>> inertial.rotation()
0.002287697
>>> inertial.rotation()
36.60607
>>> inertial.reset_rotation()
>>> inertial.rotation()
0.0007884211
>>> 
3 Likes
#include "vex.h"
  
// Allows for easier use of the VEX Library
using namespace vex;

int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
 
  Inertial2.calibrate();
  // waits for the Inertial Sensor to calibrate
  while (Inertial2.isCalibrating()) {
    wait(3000, msec);
  }
  // Turns the robot to the right
  

  double myHeading;

   
  Drivetrain.setTurnVelocity(50,percent);

  for (int i = 0; i < 10; i++) {
    
    Drivetrain.turnFor(right,90,degrees);
    //Inertial2.setRotation(0,degrees);
    Inertial2.resetRotation();
    myHeading = Inertial2.rotation(degrees);
    printf("Heading = %lf\n", myHeading);
    
   



}






}

and the Python version

from vex import *

# Begin project code
inertial2.calibrate()
wait(3,SECONDS)

drivetrain.set_turn_velocity(20,PERCENT)
count = 0
while count <= 10:

    drivetrain.turn_for(RIGHT,90,DEGREES)
    wait(15,MSEC)
    inertial2.reset_rotation()
    print(inertial2.rotation(DEGREES))
    count += 1

C++ logs

Heading = 0.000000
Heading = 0.000000
Heading = 0.000000
Heading = 0.000000
Heading = 0.000000
Heading = 0.000000
Heading = 0.000000
Heading = 0.000000
Heading = 0.000000
Heading = 0.000000

Python logs…

True
Heading: 0.0
True
Heading: 165.4795
True
Heading: 163.5055
True
Heading: 328.7216
True
Heading: 328.5153
True
Heading: 493.9607
True
Heading: 493.9681
True
Heading: 657.6947
True
Heading: 659.7081
True
Heading: 822.9315
True
Heading: 824.1383

yea, unfortunately it is a bug. reset_rotation() ( and reset_heading() ) are working on the first reset but not subsequent resets. It will need a Python VM update to fix that (meaning I’ve fixed it already), no idea what the timeline for that would be though. It affects EXP and IQ2 as well.

7 Likes

Thanks for confirming. Any thoughts on a workaround? I have competition code built in Python and everything works well, except this part, which I am using within an autonomous routine.

either avoid use of the reset_rotation() function, or track the offset in your code, for example, using some wrapper functions as follows.

rotation_offset = 0

def get_rotation():
    value = inertial.rotation() - rotation_offset
    return value

def set_rotation(value):
    global rotation_offset
    rotation_offset = 0
    rotation_offset = get_rotation() - value

def reset_rotation():
    set_rotation(0)

call get_rotation() rather than inertial.rotation() (change inertial for whatever your sensor is called) and reset_rotation() rather than inertial.reset_rotation().

The above code assumes all values are in degrees, you could add alternative units if necessary.

5 Likes

Thank you! Great suggestion and works with my autonomous routine…just finished testing

after further testing, added debug to print value of inertial.rotation within get_rotation()…it is inconsistent over time and starts returning incorrect values…causing the output value used for PID turn control to be incorrect. Also tested Python version of inertial.heading()…similar incorrect results. In C++, both worked fine in simple test program (shown above, simple loop to turn and print inertial outpus). So it seems we cannot use Python and the Inertial sensor for our PID control routine…can you point me to an example PID routine in C++…thanks

No idea why that would be. Both C++ and Python call into vexos to get the raw data.

3 Likes

We also are running into this problem…using C++ now…Inertial sensor switching direction - #4 by Nathan_Sandvig

I have ordered two new Inertial sensors but we have hit the switching direction issue twice now in the last 48 hours.