Share Pastebin
Guest
Public paste!

Robot pose (old)

By: a guest | Sep 5th, 2010 | Syntax: Java 5 | Size: 2.02 KB | Hits: 55 | Expires: Never
Copy text to clipboard
  1. package robot;
  2.  
  3. import java.io.*;
  4.  
  5. public class Pose {
  6.  
  7.     public float x, y, heading;
  8.  
  9.     public Pose () {
  10.         reset();
  11.     }
  12.  
  13.     public Pose (float x, float y, float heading) {
  14.         this.x = x;
  15.         this.y = y;
  16.         this.heading = heading;
  17.     }
  18.  
  19.     public Pose (Pose p) {
  20.         copyFrom (p);
  21.     }
  22.  
  23.     public void add (Pose delta) {
  24.  
  25.         float cos_phi = (float) Math.cos (heading);
  26.         float sin_phi = (float) Math.sin (heading);
  27.  
  28.         x += cos_phi*delta.x - sin_phi*delta.y;
  29.         y += sin_phi*delta.x + cos_phi*delta.y;
  30.         heading += delta.heading;
  31.     }
  32.  
  33.     public void subtract (Pose p) {
  34.  
  35.         float cos_phi = (float) Math.cos (p.heading);
  36.         float sin_phi = (float) Math.sin (p.heading);
  37.  
  38.         float delta_x = x - p.x;
  39.         float delta_y = y - p.y;
  40.  
  41.         x = cos_phi*delta_x + sin_phi*delta_y;
  42.         y = -sin_phi*delta_x + cos_phi*delta_y;
  43.         heading -= p.heading;
  44.     }
  45.  
  46.     public void addWheelMotion (float leftDist, float rightDist) {
  47.  
  48.         float distance = (rightDist + leftDist) / 2;
  49.         float delta_heading = (rightDist - leftDist) / (2 * Model.WHEEL_BASE);
  50.  
  51.         float cos_dphi = (float) Math.cos (delta_heading);
  52.         float sin_dphi = (float) Math.sin (delta_heading);
  53.  
  54.         float delta_x = distance + Model.REF_POINT_POS*(cos_dphi - 1);
  55.         float delta_y = Model.REF_POINT_POS * sin_dphi;
  56.        
  57.         float cos_phi = (float) Math.cos (heading);
  58.         float sin_phi = (float) Math.sin (heading);
  59.  
  60.         x += cos_phi*delta_x - sin_phi*delta_y;
  61.         y += sin_phi*delta_x + cos_phi*delta_y;
  62.         heading += delta_heading;
  63.     }
  64.  
  65.     public void reset () {
  66.         x = 0;
  67.         y = 0;
  68.         heading = 0;
  69.     }
  70.  
  71.     public void copyFrom (Pose p) {
  72.         x = p.x;
  73.         y = p.y;
  74.         heading = p.heading;
  75.     }
  76.    
  77.     public boolean equalTo (Pose p) {
  78.         return x == p.x && y == p.y && heading == p.heading;
  79.     }
  80.  
  81.     public void read (DataInputStream in) throws IOException {
  82.         x = in.readFloat();
  83.         y = in.readFloat();
  84.         heading = in.readFloat();
  85.     }
  86.  
  87.     public void write (DataOutputStream out) throws IOException {
  88.         out.writeFloat (x);
  89.         out.writeFloat (y);
  90.         out.writeFloat (heading);
  91.     }
  92.  
  93. }