Difference between revisions of "MATLAB code"
Line 7: | Line 7: | ||
== lqr_BC_optimizer.m == | == lqr_BC_optimizer.m == | ||
− | This script calculates optimal gain values. For a specified set of initial conditions, this file runs the LQR algorith on a Q matrix, and then the scaled values of that Q matrix. Then it calls runBicycleTest on each of the sets of gain values that it calculates. Based on the outputs of runBicycleTest it knows whether the bike balanced or didn't from the success output of runBicycleTest. Then it uses the allStates and applies a balance score equation to score how well those gain values worked. Then it prints out the optimal gain values based on the balance score and stores all the data into a csv file. | + | This script calculates optimal gain values. For a specified set of initial conditions, this file runs the LQR algorith on a Q matrix, and then the scaled values of that Q matrix. Then it calls runBicycleTest on each of the sets of gain values that it calculates. Based on the outputs of runBicycleTest it knows whether the bike balanced or didn't from the success output of runBicycleTest. Then it uses the allStates and applies a balance score equation and path score equation to score how well those gain values worked. Then it prints out the optimal gain values based on the balance score and stores all the data into a csv file. |
+ | |||
+ | |||
+ | ==balanceControlOptimizer== | ||
+ | This is an older script used to find optimal gain values. Instead of using LQR to find gain values to test, you manually input a range of gain values you want to test. Then the code tests these values the same way lqr_BC_optimizer does. | ||
== runBicycleTest.m == | == runBicycleTest.m == |
Revision as of 01:06, 16 May 2020
Contents
Commonly Used Variables
- state: state is an vector with 8 state variables, in order they are, time step, X position, Y position, Lean Angle, Yaw Angle, Steer Angle, Lean Angle Rate, Velocity
- motCommands - this is the same as in the control equation, for forward moving balancing, it is steer angle rate
- K - this is the vector of the three gain values k1,k2,k3. They are used to find the value of
MATLAB files
lqr_BC_optimizer.m
This script calculates optimal gain values. For a specified set of initial conditions, this file runs the LQR algorith on a Q matrix, and then the scaled values of that Q matrix. Then it calls runBicycleTest on each of the sets of gain values that it calculates. Based on the outputs of runBicycleTest it knows whether the bike balanced or didn't from the success output of runBicycleTest. Then it uses the allStates and applies a balance score equation and path score equation to score how well those gain values worked. Then it prints out the optimal gain values based on the balance score and stores all the data into a csv file.
balanceControlOptimizer
This is an older script used to find optimal gain values. Instead of using LQR to find gain values to test, you manually input a range of gain values you want to test. Then the code tests these values the same way lqr_BC_optimizer does.
runBicycleTest.m
Inputs:
- x0,y0 - initial location
- v0 - initial speed
- delta0 - initial steer angle
- phi0 - initial lean angle
- phi_dot0 - initial lean angle ratw
- psi0 - initial yaw angle (heading)
- K - vector of gains (k1, k2, k3)
- delta_offset - either a scalar (a constant offset), or a vector of offsets (the bike will attempt to hit delta_offset(n) at the nth timestep
- numTimeSteps - the number of time steps to run the simulation for
- graph - 1=draws graph, 0=does not
Outputs: success - 1 if bike stayed up successfully throughout duration of the test, 0 if the bike falls down(lean angle becomes greater than pi/4 during the test) allStates - matrix with size (number of time steps x 8). Each row of this matrix contains a state, it contains all the states for all the timesteps in the test.
This function uses state variables and gains and passes them to rhs, which produces the derivative of all the state variables(minus timestep) and the motor command then it uses that derivative and euler integrates to find the next state values. It repeats this process for all the time steps in the test. Each time step is 1/50 second apart. Finally it uses all the states and all the motor commands and calls animateBike, to produce an animation of the bike and create all the data graphs for analysis.
rhs.m
- currentState - contains the 8 current state variables
- p - contains bike parameters .............
- K - Gain values
- delta_offset - desired steer angle
- phi_offset - desired lean angle
Outputs:
- zdot - zdot is a vector of the 7 derivatives of the state variables(minus timestep), it is used in the euler integration in runBicycleTest
- u - u is the same as the motor command, it is the that will be applied to the bike so that the bike can balance itself
rhs works by using the gains values to calculate u, and then using the EOM to calculate the derivatives. It also sets a max steer angle rate to 4.8 rad/s based on the limitations of the motor used. For straight line balancing, delta_offset and phi_offset will be 0. For turning balancing, they will be determined by the navigation controller. rhs calls no other functions.
BikeAndMotorConstants.m
No inputs Output: CONST - A vector containing important constants about the bike
This function calculates important constants including moment of inertia, center of mass, and others. It is called in drawBike.
animateBike.m
Inputs:
- state
- p
- motCommands
- delta_offset
- phi_offset
balanceControlOptimizer.m
CircleAboutZ.m
Inputs:
- r - radius of the circle
drawBike.m
Inputs:
- x
- y
- z
- yaw
- roll
- steer
- p