Close

Lean angle calculation method

A project log for Motorcycle lean angle video overlay

A project to add lean angle overlay to your motorcycle videos

simonSimon 09/05/2016 at 08:563 Comments

This log will give a short description of how the estimate of motorcycle lean angle is calculated from GPS NMEA sentences.

Starting with the GPS module itself I'm using the second (TX only I believe) UART on the esp8266 to send some bytes to configure the Ublox-Neo-6M GPS module to only output the GPRMC sentence and to set the update rate to 5Hz. I had previously done some tests using my phone to record NMEA sentences which could only do a 1Hz update rate. 5Hz is working out much better.

With the GPS outputting GPRMC at 5Hz the serial data is passed to the Openlog and recorded on the SD card for processing later.

Ok so now the post processing. Basically the calculation is dependent on two parameters: speed and track bearing.

The first step is to determine if the bike is turning left or right based on the "current" value and the "previous" value of the track bearing. "Turns" (haha) out this is very easy by doing a cross product like so:

The value of turn will vary between 1 and -1 where turn > 0 is a right hand turn and turn < 0 is a left hand turn. This is used to to assign positive and negative values to right and left turns respectively.

Next is to determine the rate of change of direction, yaw rate. This is fairly easy most of the time i.e.

However when the points cross through zero degrees (North) a bit of extra care is required. This is probably a well known algorithm but it was instructive to figure it out myself.

if (abs(yaw_delta) > (180))
{
    yaw_delta = turn * ((360) - abs(yaw_delta));
}

So now we know how many degrees the bike has turned through between the current and the previous data point and we know if the bike is turning left or right. The period of the turn, if it was to come full circle can be calculated. Like so:

Where the datarate is the data rate of the GPS, set to 5Hz in my case. The assumption is made that the path of the bike through the turn follows an arc of a circle. This is probably not a bad assumption given that the time elapsed between data points is 0.2 seconds and the radius of the corner is going to be quite large. The radius of the path through the turn is given by:

Where the speed is in m/s.

Finally the lean angle in degrees is given by:

Well that's a quick tour through the calculation method I've used. I hope it is useful to someone.

Discussions

Nikolay Kolchenko wrote 12/14/2016 at 15:33 point

Definitely! Thank you. Actually I'm trying to do the same :-)

  Are you sure? yes | no

Nikolay Kolchenko wrote 12/10/2016 at 10:38 point

Just a quick question. Why did you prefer to go with GPS sensor + some Math instead of Gyro sensor (like MPU6050) ?  Cost? Complexity? Need to log GPS data? 

BTW, the following info : http://www.stevemunden.com/leanangle.html may be useful for all these who areinterested in converting speed/radius into lean angle

  Are you sure? yes | no

Simon wrote 12/13/2016 at 10:42 point

Hi, thanks for the question. A few things contributed to selecting GPS. 

Firstly I prototyped some of the overlay generating code from GPS data I logged using an app on my phone. This was exciting because I could get to the fun part before having to figure out how to build the hardware. 

Second, I'm sure a gyro can be used but I'm not really sure if the drift can still be (or needs to be) accounted for by using an accelerometer and a Kalman (or similar) filter. I haven't made a project with a gyro or accel before so in a way I was sticking with what I knew would work.


I did have the idea to include a proper 9 axis IMU and fuse the data with GPS to produce a better estimate of the state of the motorcycle. This would be an opportunity for a lot of learning for me...

Having said all of this, it recently occurred to me that the main win from this project is the development of a method to log time syncronised GPS with GoPro. As far as I know there is no good way of doing this. Other than to eyeball the data and video at the final stage of editing the video. The fact that I'm using the GPS data to calculate an estimate of lean angle is somewhat irrelevant. Anyway, I hope that answers your question.

  Are you sure? yes | no