Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

AHRS fusion with circuit python
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

AHRS fusion with circuit python

by sminogue on Sun Oct 04, 2020 9:50 am

I am just getting started with both the board (Feather Sense) and Circuit Python so please forgive what I hope is a complete newb question. I am looking to pull motion data from my board, so following instructions on the circuitpython sense demo I got the board up and running and dumping sensor data to the... console? terminal? I then went on to follow Lady Ada's "How to Fuse Motion Sensor Data into AHRS Orientation" tutorial. Once I got past the transition from circuitpython to using the Arduino IDE it went well and at the end I had a rabbit twisting around on the screen.

My question is how do I do all this using Circuit Python? Ive been searching around and searching this forum but not finding how to do the sensor fusion in Circuit Python?

Im interested in moving forward in CP because it seems like the develop/test loop is shorter, simply saving the .py file on the board and see results as opposed to the compile and upload going on in the Arduino IDE. Also, frankly I havent touched C since college while I have dabbled with Python so am more comfortable with it.

sminogue
 
Posts: 11
Joined: Sun Oct 04, 2020 9:35 am

Re: AHRS fusion with circuit python

by siddacious on Mon Oct 05, 2020 1:16 pm

OOh, sounds like fun! I agree with your take on CP's iteration times, they're quite handy for developing code.

To my knowledge, there aren't any existing AHRS libraries written specifically for circuitpython, but since we try to keep CircuitPython as close as possible to CPython, it should be possible to port a pure-python AHRS library to CircuitPython. Alternatively you could translate the Arduino code into CircuitPython.

siddacious
 
Posts: 338
Joined: Fri Apr 21, 2017 3:09 pm

Re: AHRS fusion with circuit python

by sminogue on Fri Oct 09, 2020 9:50 am

Thanks. I have decided to shelve this for now. I just dont have the time to dive into converting something to CP. I did see an AHRS in python out there but it makes use of NumPi... which is great but I worry about loading more libraries onto the board and eating up space. Also, I'm concerned that making the AHRS be done in python could be a performance issue as the python is interpreted rather than compiled. Basically I cant spend the hours going down the python route to have to backtrack and do it in C++ anyway due to performance problems or size constraints.

I did spot (but didnt delve into) calling C++ functions from CircuitPython. Any idea how this works or where they would be more information about doing it? Might just be easier to leave the AHRS in c++ and call it from circuitpython code.

sminogue
 
Posts: 11
Joined: Sun Oct 04, 2020 9:35 am

Re: AHRS fusion with circuit python

by sminogue on Mon Oct 12, 2020 12:10 pm

So a followup question. I have been working on the AHRS stuff in cpp and its going well, but I am having some issues with calibration. As I had mentioned i went through the AHRS tutorial where I did calibration with MotionCal... Seems to have worked... but now what I am interested in is not using motion cal.

I see Adafruit_Sensor_Calibration has a calibrate function and I am using it. :
Code: Select all | TOGGLE FULL SIZE

#if defined(ADAFRUIT_SENSOR_CALIBRATION_USE_EEPROM)
  Adafruit_Sensor_Calibration_EEPROM cal;
#else
  Adafruit_Sensor_Calibration_SDFat cal;
#endif

void setup() {
... init sensors, load saved calibration ect...
}

void loop() {

  sensors_event_t accel, gyro, mag;
  accelerometer->getEvent(&accel);
  gyroscope->getEvent(&gyro);
  magnetometer->getEvent(&mag);

  cal.calibrate(mag);
  cal.calibrate(accel);
  cal.calibrate(gyro);

... Do AHRS stuff here ...

if (! cal.saveCalibration()) {
    Serial.println("**WARNING** Couldn't save calibration");
  } else {
    Serial.println("Wrote calibration");   
  }
    cal.printSavedCalibration();

}


So I am cutting out a lot of my program logic obviously but the idea is that as the code runs it continues to refine the calibration. In the real code I have some logic that causes it to only read the sensors every like 15ms then some more logic that limits how many iterations should go by before it saves the calibration to "disk". But for brevity I have removed all that and its pretty well tested anyway.

So testing... I took my new board and formatted the memory. I then used the calibration examples to write a starting calibration to memory. I then pushed my program and let it run while twisting and rotating my sensor. Watching the serial monitor I am seeing a series of "Wrote Calibration" messages followed by the actual saved calibration... The problem is that the calibration being printed never changes from the initial default values I had first stored in memory. So while I am calling the calibrate function and apparently successfully storing the calibration in memory... The calibration is unchanging.

Any idea whats going on?

sminogue
 
Posts: 11
Joined: Sun Oct 04, 2020 9:35 am

Please be positive and constructive with your questions and comments.