Answers.Body: <p>What you're trying to accomplish is called <a href="https://en.wikipedia.org/wiki/Odometry" rel="nofollow noreferrer">Odometry</a>, and fortunately it's a pretty common task in robotics so there's a lot of information available on how to do it. The short answer is, it's relatively easy to implement, but hard to make it work really well.  </p>
  
  <p>Let's make things simpler by deciding that the robot can only a) drive straight and b) turn 90 degrees. In other words, the robot reaches an x,y coordinate only by driving along the cardinal directions N, S, E, W. We can make this simplification to avoid having to use trigonometry.  </p>
  
  <p>Now we need some variables to keep track of a) where the robot is, and b) what direction it's facing.  </p>
  
  <ul>
  <li><p>Make a variable "orientation" to remember which direction the robot is facing. 1=N, 2=E, 3=S, 4=W, for example.  </p></li>
  <li><p>Make variables "X" and "Y" to remember the position. These can represent whatever units of length you want. Let's just say X and Y represent motor rotations, for simplicity.  </p></li>
  </ul>
  
  <p>When the robot moves straight, you should update the coordinates X or Y depending on which direction it is facing. For example, if the robot drives 3 rotations fwd:</p>
  
  <ul>
  <li>If orientation==1 (North), increase Y by 3 (rotations).</li>
  <li>If orientation==2 (East), increase X by 3</li>
  <li>If orientation==3 (South), decrease Y by 3</li>
  <li>If orientation==4 (West), decrease Y by 3.  </li>
  </ul>
  
  <p>When the robot turns 90 degrees, you should update the orientation variable accordingly. See my other post for <a href="https://bricks.stackexchange.com/questions/10466/how-to-make-an-exact-16-degree-turn/10482#10482">how to rotate your robot a specific number of degrees</a>. Assuming a clockwise rotation,</p>
  
  <ul>
  <li>If orientation==1 (North), set orientation==2 (East).</li>
  <li>If orientation==2 (East),  set orientation==3 (South).</li>
  <li>If orientation==3 (South), set orientation==4 (West).</li>
  <li>If orientation==4 (West),  set orientation==1 (North).</li>
  </ul>
  
  <p>Assuming you <em>always</em> update your coordinates and orientation whenever you move, your robot should be able to (roughly) keep track of its position using this method. You can make it easier for yourself by putting the DriveStraight and Turn90 code into functions (MyBlocks, if you're using the standard Lego software). For example, the DriveStraight function/MyBlock should take the desired number of rotations, drive the robot that far, and update the global X/Y variables. Similar for Turn90. Then your entire program consists of DriveStraight and Turn90 blocks to move around, and at any time you can read the X,Y coordinate and orientation from your global variables.  </p>
  
  <p>One problem with this method is that for every movement your robot makes, error accumulates, until eventually its internal position is no longer accurate. The easiest way to account for this is to occasionally return to a known physical location - for example, a wall corner at 0,0 - and reset X,Y to 0,0 when it gets there. A lot of FLL robots use this technique - these robots have square edges and back themselves into corners to force themselves into a known absolute position/orientation.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-18T15:22:26.963Z
Answers.LastActivityDate: 2019-04-18T15:22:26.963Z
Answers.UserDisplayName: MindS1
Body: <p>I'm trying to make a internal coordinate system for an EV3 Python project I'm doing, but at a complete loss on how to do it. It needs to keep track of how it moves (with wheels) in an X, Y coordinate system. The space that the project operates in will only have the robot move in mainly X and Y directions.</p>
  
Tags: <ev3><mindstorms><python>
Title: How to make a point coordinate system for EV3
UserDisplayName: ryn0129
ThreadText: # How to make a point coordinate system for EV3
  
  (Asked by: username_0 on 2019-04-17)
  
  I'm trying to make a internal coordinate system for an EV3 Python project I'm doing, but at a complete loss on how to do it. It needs to keep track of how it moves (with wheels) in an X, Y coordinate system. The space that the project operates in will only have the robot move in mainly X and Y directions.
  
  **Tags:** ev3, mindstorms, python
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-04-18. Score: 2
  
  What you're trying to accomplish is called [Odometry](https://en.wikipedia.org/wiki/Odometry), and fortunately it's a pretty common task in robotics so there's a lot of information available on how to do it. The short answer is, it's relatively easy to implement, but hard to make it work really well.
  
  Let's make things simpler by deciding that the robot can only a) drive straight and b) turn 90 degrees. In other words, the robot reaches an x,y coordinate only by driving along the cardinal directions N, S, E, W. We can make this simplification to avoid having to use trigonometry.
  
  Now we need some variables to keep track of a) where the robot is, and b) what direction it's facing.
  
  * Make a variable "orientation" to remember which direction the robot is facing. 1=N, 2=E, 3=S, 4=W, for example.
  * Make variables "X" and "Y" to remember the position. These can represent whatever units of length you want. Let's just say X and Y represent motor rotations, for simplicity.
  
  When the robot moves straight, you should update the coordinates X or Y depending on which direction it is facing. For example, if the robot drives 3 rotations fwd:
  
  * If orientation==1 (North), increase Y by 3 (rotations).
  * If orientation==2 (East), increase X by 3
  * If orientation==3 (South), decrease Y by 3
  * If orientation==4 (West), decrease Y by 3.
  
  When the robot turns 90 degrees, you should update the orientation variable accordingly. See my other post for [how to rotate your robot a specific number of degrees](https://bricks.stackexchange.com/questions/10466/how-to-make-an-exact-16-degree-turn/10482#10482). Assuming a clockwise rotation,
  
  * If orientation==1 (North), set orientation==2 (East).
  * If orientation==2 (East), set orientation==3 (South).
  * If orientation==3 (South), set orientation==4 (West).
  * If orientation==4 (West), set orientation==1 (North).
  
  Assuming you *always* update your coordinates and orientation whenever you move, your robot should be able to (roughly) keep track of its position using this method. You can make it easier for yourself by putting the DriveStraight and Turn90 code into functions (MyBlocks, if you're using the standard Lego software). For example, the DriveStraight function/MyBlock should take the desired number of rotations, drive the robot that far, and update the global X/Y variables. Similar for Turn90. Then your entire program consists of DriveStraight and Turn90 blocks to move around, and at any time you can read the X,Y coordinate and orientation from your global variables.
  
  One problem with this method is that for every movement your robot makes, error accumulates, until eventually its internal position is no longer accurate. The easiest way to account for this is to occasionally return to a known physical location - for example, a wall corner at 0,0 - and reset X,Y to 0,0 when it gets there. A lot of FLL robots use this technique - these robots have square edges and back themselves into corners to force themselves into a known absolute position/orientation.
  
  
  ---
  
  