{"id":561,"date":"2013-05-27T14:13:28","date_gmt":"2013-05-27T04:13:28","guid":{"rendered":"http:\/\/legoeng.local\/?p=561"},"modified":"2022-08-01T10:04:15","modified_gmt":"2022-08-01T00:04:15","slug":"measuring-area-with-a-robot-part-one-follow-the-path","status":"publish","type":"post","link":"http:\/\/legoeng.local\/measuring-area-with-a-robot-part-one-follow-the-path\/","title":{"rendered":"Area Measurement: Follow the Path"},"content":{"rendered":"

I’ve often wanted to see greater educational use of LEGO robots more in alignment with how robots get used in the real world – taking on that dull, dirty, dangerous, or distant task.<\/p>\n

One approach could be to measure an area, map the features, and then conduct missions based on the calculated map.  The idea sounds a bit challenging for a LEGO robot, but not if we take it in bite-sized pieces. Let’s start with measuring area while following a line.<\/p>\n

If we can provide a frame of reference for the robot, then we can calculate how far we’ve gone in any direction with a little odometry and trigonometry.<\/p>\n

The easiest frame of reference I found is to use a magnetic compass and set magnetic North to be the Y-axis on a cartesian coordinate system.  I also assume the robot begins its travel from the origin: (0,0).<\/p>\n

When the robot moves, the distance it travels becomes the hypotenuse of a right triangle whose legs are the North (y-axis) and East (x-axis) components. The new robot position can then be calculated by adding the North components (old y position + additional travel in the y direction) and the East components (old x position + additional travel in the x direction).<\/p>\n

When written as equations, this becomes:<\/p>\n

X new<\/sub> = X old <\/sub>+  <\/b>\u0394X<\/b><\/p>\n

Y new<\/sub> = Y old <\/sub>+  <\/b>\u0394Y<\/b><\/p>\n

Where do the \u0394X and \u0394Y come from? They come from multiplying the distance traveled and the sine or cosine of the robot’s heading. In equations:<\/p>\n

\u0394X = Dist *Sine (Hdg) <\/b><\/p>\n

\u0394Y = Dist *Cosine (Hdg)<\/b><\/p>\n

Programmers get to choose how far between each coordinate pair they want to let the robot travel before calculating the new (x,y) coordinate pair. In LEGO robots, that value usually comes from odometry based on wheel rotations.<\/p>\n

Here’s a graphic summarizing the creation of new (x,y) coordinates for one leg of travel:<\/p>\n

\"Components<\/a>
Components of Robot Travel<\/figcaption><\/figure>\n

Here’s a graphic summarizing the creation of (x,y) coordinate pairs for five legs of robot travel along the line:<\/p>\n

\"Multiple<\/a>
Multiple Portions of Robot Travel – and their components<\/figcaption><\/figure>\n

When doing this the first few times, it will probably be easier to write the (x,y) coordinate pairs to a file and then calculate the area off line, which I’ll explore next.<\/p>\n

Calculating exact area from continuous curves perfectly usually requires double integrals.  But our robot is converting the smooth boundary it’s following into a series of (x,y) coordinate pairs connected by straight lines.  There’s a method to measure area within any polygon bounded by straight lines.  Here it is:<\/p>\n

    \n
  1. Beginning with any vertex, list the coordinates of the vertices in order, moving counter-clockwise around the polygon.       List the first pair again at the end.<\/li>\n
  2. Find the diagonal products from left to right.<\/li>\n
  3. Find the diagonal products from right to left.<\/li>\n
  4. Sum each column of products.<\/li>\n
  5. Find their difference and divide by 2.  This is the polygon\u2019s area.<\/li>\n<\/ol>\n

    Here’s a graphic showing the approach:<\/p>\n

    \"Area<\/a>
    Area the Easy Way<\/figcaption><\/figure>\n

    I’ve implemented the approach using a line following robot by writing the calculated (x,y) pairs to a data file and then bringing them into a spreadsheet for plotting and calculating.<\/p>\n

    Here’s a graphic showing the results:<\/p>\n

    \"Area<\/a>
    Area Plots<\/figcaption><\/figure>\n

    They’re not perfect, but should be good enough for most classroom applications.  The red arrows indicate the difference between the origin \/ robot starting position and its endpoint.  Both points at the tips of the red arrows are actually coincident and result (most likely) from odometry errors in the robot’s distance calculations.<\/p>\n

    Pitfalls<\/h2>\n

    But what about South and West<\/b>?  Think of South as negative North<\/i> and West as negative East.<\/i><\/p>\n

    Odometry errors<\/b>. For most applications with LEGO robots, using the values LEGO provides on the side of the tire is probably sufficient.  Similarly, a measured wheelbase from tire center to opposite tire’s center is usually sufficient.  But,  when we start into more precise measurements, those approximations become apparent as they introduce error.  There are a couple of ways to reduce this error.<\/p>\n