{"id":303,"date":"2013-08-01T19:29:38","date_gmt":"2013-08-01T19:29:38","guid":{"rendered":"http:\/\/blog.daft-ideas.co.uk\/?p=303"},"modified":"2013-08-01T19:43:58","modified_gmt":"2013-08-01T19:43:58","slug":"uav-reading-sensor-data","status":"publish","type":"post","link":"https:\/\/blog.daft-ideas.co.uk\/2013\/08\/01\/uav-reading-sensor-data\/","title":{"rendered":"UAV, reading sensor data"},"content":{"rendered":"

A long time before we did our session where we controlled some servos, we had a session where we were reading sensor data.<\/p>\n

We’ve tried this a number of times, but this instance was where we actually started to get worthwhile data, and we could interpret it correctly.<\/p>\n

We are currently reading two sensors to find orientation data: the 3 axis gyroscope and the 3 axis accelerometer.<\/p>\n

I’ll cover the accelerometer first.<\/p>\n

Reading\u00a0Accelerometer\u00a0Data<\/h2>\n

We’re using a 3 axis\u00a0accelerometer\u00a0 which if read correctly can tell us the pitch and roll of the aircraft (actually, just the pitch and roll of the sensor, but the sensor will be built into the aircraft).<\/p>\n

Each axis in the accelerometer (we’ll call them accX, accY and accZ) tells us how pull that sensor is experiencing (at rest, this is due to gravity). We only need 1 axis to start with, to measure Pitch.<\/p>\n

\"Pitch\"<\/a>In this crappy diagram, the rectangle represents our accelerometer. It’s measuring apparent gravity. As the acceleromter has its pitch increased (rotated clockwise, in this case), the apparent gravity being measured in that axis will decrease.<\/p>\n

Theta represents the angle between the apparent gravity and actual gravity.<\/p>\n

The trigonometery to calculate pitch is pretty easy:<\/p>\n

apparent gravity = cos (theta) * g<\/em><\/p>\n

or:<\/p>\n

acc = cos (theta) * g<\/em><\/p>\n

The relationship between theta and pitch is very simple: simply add 90 degrees to theta to get pitch.<\/p>\n

So, rewrite<\/p>\n

apparent gravity = cos (theta) * g\u00a0<\/em><\/p>\n

as<\/p>\n

theta = acos (acc \/ g)<\/em><\/p>\n

And then to get the actual pitch:<\/p>\n

pitch = asin (acc \/ g)<\/em><\/p>\n

\/*------------------------------------------------------------------------*\/\r\n\/\/Accellerometer Read and Output Section\r\n\r\n  float xAccRate, yAccRate, zAccRate;\r\n  double pitch, roll;\r\n\r\n  xAccRate = (accReadX());\r\n  yAccRate = (accReadY());\r\n  zAccRate = (accReadZ());\r\n\r\n  double measured_g = sqrt((xAccRate*xAccRate)+(yAccRate*yAccRate)+(zAccRate*zAccRate));\r\n\r\n  roll = (atan2(xAccRate\/128,zAccRate\/128))*(180\/3.141);\r\n  pitch = (atan2(yAccRate\/128,zAccRate\/128))*(180\/3.141);<\/pre>\n

Reading\u00a0Gyro\u00a0Data<\/h2>\n

Gyro data is actually a lot easier to read, in some ways. All a the gyrometer is doing is measuring rate of change since the last reading.<\/p>\n

Therefore, all we need to do is integrate over a period of time to get the rotation in a certain axis.<\/p>\n

Code:<\/p>\n

\/*-----------------------------------------------------------------------------*\/\r\n\/\/Gyro Read & Output Section\r\n\r\n  \/\/Create variables for outputs\r\n  float xGyroRate, yGyroRate, zGyroRate, xGyroRate2, yGyroRate2, zGyroRate2;\r\n  long Time;\r\n  \/\/Read the x,y and z output rates from the gyroscope & correct for some innacuracy; convert to seconds\r\n  xGyroRate = (gyroReadX())\/57.5;\r\n  yGyroRate = (gyroReadY())\/57.5;\r\n  zGyroRate = (gyroReadZ())\/57.5;\r\n  \/\/Determine how long it's been moving at this 'rate', in seconds\r\n  Time = (millis()-previousMillis);\r\n  \/\/Multiply rates by duration\r\n  xGyroRate2 = -(xGyroRate\/Time)\/4;\r\n  yGyroRate2 = -(yGyroRate\/Time)\/4;\r\n  zGyroRate2 = -(zGyroRate\/Time)\/4;\r\n  \/\/Add to cumulative figure\r\n  if (((xGyroRate2)>(gyroLPF))||((xGyroRate2)<(-gyroLPF)))   CumulatGyroX += (xGyroRate2);   if (yGyroRate2>gyroLPF||yGyroRate2<-gyroLPF)   CumulatGyroY += (yGyroRate2);   if (zGyroRate2>gyroLPF||zGyroRate2<-gyroLPF)\r\n  CumulatGyroZ += (zGyroRate2);<\/pre>\n

Pretty simple \ud83d\ude42<\/p>\n

Some pretty graphs<\/h2>\n

Of course, that code took us a long time to actually write. And we may have borrowed some from elsewhere, I can’t for the life of me remember. Once we’d got it working, we plumbed the serial output into GNUPlot, so that we could get a visual representation of what was going on.<\/p>\n

The general idea was to tilt the board by 90 degrees in once axis, then return it to it’s original position. This would produce a sine wave, eg:<\/p>\n

\"output2\"<\/a><\/p>\n

After mucking about for a bit with minicom, I used this command to capture the serial output:<\/p>\n

sudo minicom --capture mincap<\/pre>\n

The serial output just looks something like this:<\/p>\n

ID: 69\r\n2 0 0\r\n-32 -4 -27\r\n-1 -1 1\r\n0 -1 1\r\n0 0 1\r\n0 -1 0\r\n0 1 0\r\n0 1 0\r\n1 -1 0\r\n1 -1 0\r\n0 -2 0\r\n0 -1 0<\/pre>\n

Once we’d got that, I spend yet more time mucking around in gnuplot. Actually, I jest. It was pretty easy to plot a graph.<\/p>\n

The first graph we got was this:<\/p>\n

\"We'd<\/a>

We’d done something very obvious and simple wrong.<\/p><\/div>\n

Spot the deliberate mistake.<\/p>\n

Upon seeing this, we retired to the pub. Over a pint or three, we realised what it was we’d done wrong.<\/p>\n

The Arduino board has an integer size of 16 bit, meaning it has a max size of\u00a032,767. If you try to do<\/p>\n

int i = 32767 + 1;<\/p>\n

then i would have the value -32767.<\/p>\n

And that’s exactly what was happening. We’d used an Integer, because occasionally I’m as thick as a brick sandwich.<\/p>\n

We weren’t doing anything with the data yet to convert it to degrees, and we weren’t even sure how big the numbers would be, and we were just adding it all up.<\/p>\n

Here’s another graph showing the same thing, but with the dots joined up:<\/p>\n

\"temp2\"<\/a><\/p>\n

After we started using the (entirely more sensible) long data type instead, we got what we’d originally expected:<\/p>\n

\"temp6\"<\/a><\/p>\n

So, as predicted, we got ourselves half a sine wave. If we tilt it by 90 degrees in one direction, then back to flat, then 90 degrees in the other direction, we get the full sine wave:<\/p>\n

\"If<\/a>

If the graph is a bit lumpy, it’s because Matt had the shakes.<\/p><\/div>\n

In the next post, I’ll talk about working out the actual orientation, and sensor drift. \u00a0We’re also going to compare the output from the accelerometer and gyro.<\/p>\n","protected":false},"excerpt":{"rendered":"

A long time before we did our session where we controlled some servos, we had a session where we were reading sensor data. We’ve tried this a number of times, but this instance was where we actually started to get worthwhile data, and we could interpret it correctly. We are… Continue reading → <\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,9],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.daft-ideas.co.uk\/wp-json\/wp\/v2\/posts\/303"}],"collection":[{"href":"https:\/\/blog.daft-ideas.co.uk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.daft-ideas.co.uk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.daft-ideas.co.uk\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.daft-ideas.co.uk\/wp-json\/wp\/v2\/comments?post=303"}],"version-history":[{"count":11,"href":"https:\/\/blog.daft-ideas.co.uk\/wp-json\/wp\/v2\/posts\/303\/revisions"}],"predecessor-version":[{"id":419,"href":"https:\/\/blog.daft-ideas.co.uk\/wp-json\/wp\/v2\/posts\/303\/revisions\/419"}],"wp:attachment":[{"href":"https:\/\/blog.daft-ideas.co.uk\/wp-json\/wp\/v2\/media?parent=303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.daft-ideas.co.uk\/wp-json\/wp\/v2\/categories?post=303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.daft-ideas.co.uk\/wp-json\/wp\/v2\/tags?post=303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}