Rose-Hulman Robotics Team

Changeset 737 for trunk

Show
Ignore:
Timestamp:
05/09/10 13:17:28 (21 months ago)
Author:
kleinjt
Message:

added work in progress kalman filter

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/software/rb/kalman.py

    r718 r737  
    1 # Copyright (C) 2009 Thomas W. Most 
    2 # 
     1# Copyright (C) 2010 Thomas W. Most, Jon Klein 
     2#  
    33# This program is free software: you can redistribute it and/or modify 
    44# it under the terms of the GNU General Public License as published by 
     
    1414# along with this program.  If not, see <http://www.gnu.org/licenses/>. 
    1515 
     16# ___________________________________  
     17#/ I am wearing a hat, so I'm only   \ 
     18#\ probably a stegosaurus.           / 
     19# -----------------------------------  
     20#\                             .       . 
     21# \                           / `.   .' "  
     22#  \                  .---.  <    > <    >  .---. 
     23#   \                 |    \  \ - ~ ~ - /  /    | 
     24#         _____          ..-~             ~-..-~ 
     25#        |     |   \~~~\.'                    `./~~~/ 
     26#       ---------   \__/                        \__/ 
     27#      .'  O    \     /               /       \  "  
     28#     (_____,    `._.'               |         }  \/~~~/ 
     29#      `----.          /       }     |        /    \__/ 
     30#            `-.      |       /      |       /      `. ,~~| 
     31#                ~-.__|      /_ - ~ ^|      /- _      `..-'    
     32#                     |     /        |     /     ~-.     `-. _  _  _ 
     33#                     |_____|        |_____ 
     34 
     35# for the theory beind this file, and if you want to know what the stegosaurus is talking about,  
     36# see Mobile Robot Localization using Kalman Filters by Jon Klein, Trenton Tabor 
     37# http://tarsonis.dhcp.rose-hulman.edu/robot_kalman.pdf 
     38 
    1639 
    1740from thread import start_new_thread 
     
    1942from rb.utils import meter_offset 
    2043 
     44import pylab 
     45from numpy import * 
     46 
     47# constants for state matrix 
     48LONGITUDE = 0 
     49LATITUDE = 1 
     50LOCATION = 0:2 
     51LINEAR_VELOCITY = 2 
     52ORIENTATION = 3 
     53ANGULAR_VELOCITY = 4 
     54 
     55STATE_SIZE = 5                   # number of dimensions tracked in the state matrix 
     56GPS_TIMESTEP = 1                 # update rate for GPS 
     57COMPASS_TIMESTEP = .1            # update rate for compass 
     58MAX_LINEAR_ACCELERATION = 9.8    # maximum possible linear acceleration 
    2159 
    2260class KalmanFilter(object): 
     
    4482                self.gps_zero = None 
    4583                 
    46                 # Raw sensor data for use before the filter has actually 
    47                 # been implemented 
    48                 self._x_y = (0, 0) 
    49                 self._orient = 0 
    50                 # TODO 
     84                # state matrix, see (10), initialize states to zero 
     85      self.state_matrix = mat(zeros((STATE_SIZE,1))) 
     86      self.state_matrix[LOCATION] = 0 
     87      self.state_matrix[LINEAR_VELOCITY] = 0 
     88      self.state_matrix[ANGULAR_VELOCITY] = 0 
     89      self.state_matrix[ORIENTATION] = 0 
     90       
     91      # constant modeling error matrix Q, see (11-15) 
     92      self.modeling_error = mat(zeros((STATE_SIZE,STATE_SIZE))) 
     93      location_error = (GPS_TIMESTEP ** 2) * MAX_LINEAR_ACCELERATION / 2 
     94      orientation_error = (COMPASS_TIMESTEP ** 2) * MAX_ANGULAR_ACCELERATION / 2 
     95      self.modeling_error[LATITUDE,LATITUDE] = location_error 
     96      self.modeling_error[LONGITUDE,LONGITUDE] = location_error 
     97      self.modeling_error[LINEAR_VELOCITY,LINEAR_VELOCITY] = ENCODER_TIMESTEP * MAX_LINEAR_ACCELERATION 
     98      self.modeling_error[ANGULAR_VELOCITY, ANGULAR_VELOCITY] = ENCODER_TIMESTEP * MAX_ANGULAR_ACCELERATION 
     99      self.modeling_error[ORIENTATION, ORIENTATION] = orientation_error  
     100       
     101      # noise model matrix R, let's assume we begin with no noise, see (19) 
     102      self.measurement_noise = mat(zeros((STATE_SIZE,STATE_SIZE))) 
     103       
     104      # boring, identity matrix 
     105      self.identity = mat(eye(STATE_SIZE,STATE_SIZE)) 
     106 
     107      # transistion matrix, see (16) 
     108      self.transistion = mat(eye(STATE_SIZE, STATE_SIZE); 
     109       
     110   def _update_(self)  
     111   def _update_transistion_matrx(self) 
     112      ''' 
     113      Update the transistion matrix to take into accout changes in one dimension effecting changes in another dimension 
     114      See robot model, (4.4) 
     115      ''' 
     116 
     117      self.transistion(ORIENTATION,ANGULAR_VELOCITY) = ENCODER_TIMESTEP 
     118      self.transistion(LATITUDE, VELOCITY) = ENCODER_TIMESTEP * cos(self.state_matrix(ANGULAR_VELOCITY)) 
     119      self.transistion(LONGITUDE, VELOCITY) = ENCODER_TIMESTEP * sin(self.state_matrix(ANGULAR_VELOCITY)) 
    51120 
    52121        def update_target_speed(self, data): 
     
    54123                Update filter with the target speed. 
    55124                ''' 
    56          
     125       
    57126        def update_encoders(self, data): 
    58127                ''' 
    59128                Update the filter with encoder data. 
     129                data.left is change in distance, in meters, from the left encoder 
     130                data.right is the change in distance, in meters, from the right encoder 
    60131                ''' 
    61          
     132      # updates the angular and linear velocity 
     133      # see (2.1.3), 
     134       
     135      H = mat(zeros((STATE_SIZE,1))); 
     136      H[LINEAR_VELOCITY] = 1 
     137      H[ANGULAR_VELOCITY] = 1 
     138 
    62139        def update_microstrain(self, accel): 
    63140                ''' 
    64141                Update the filter with data from the Microstrain. 
    65142                ''' 
    66                 self._orient = accel.orient[2] 
     143       
     144      H = mat(zeros((STATE_SIZE,1))) 
     145      H[ORIENTATION] = 1 
     146                 
     147                self._orient = accel.orient[2]  
    67148         
    68149        def update_gps(self, coords): 
     
    70151                Update the filter with data from the GPS. 
    71152                ''' 
     153                 
     154      H = mat(zeros((STATE_SIZE,1))); 
     155      H[LONGITUDE] = 1 
     156      H[LATITUDE] = 1 
     157      
    72158                if self.gps_zero is None: 
    73159                        # Use the robot's initial location as zero 
    74160                        self.gps_zero = coords.lat_lon 
    75161                self._x_y = meter_offset(self.gps_zero, coords.lat_lon) 
    76          
     162 
     163   def measurement_update(self, H) 
     164      ''' 
     165       
     166      ''' 
     167 
     168 
    77169        def update_drive_speed(self, (lspeed, rspeed)): 
    78170                ''' 
    79171                Update the filter with data based on the drive motors.  Calculate the 
    80172                rotational velocity and change in position based on dead reckoning. 
     173                Not actually used for anything, you may not want to call this function. .  
    81174                ''' 
    82175                pass # TODO