Goes in a Line!

Dependencies:   AVEncoder mbed-src-AV

Fork of Test by 2015-2016_Mouserat

Committer:
jimmery
Date:
Sat Nov 21 03:20:07 2015 +0000
Revision:
0:13d8a77fb1d7
Child:
1:3cacc3c50e68
Child:
2:98efd8dd9077
pid test.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jimmery 0:13d8a77fb1d7 1 #include "mbed.h"
jimmery 0:13d8a77fb1d7 2 #include "AVEncoder.h"
jimmery 0:13d8a77fb1d7 3
jimmery 0:13d8a77fb1d7 4 // set things
jimmery 0:13d8a77fb1d7 5 Serial pc(SERIAL_TX, SERIAL_RX);
jimmery 0:13d8a77fb1d7 6 Ticker Systicker;
jimmery 0:13d8a77fb1d7 7 Timer timer;
jimmery 0:13d8a77fb1d7 8
jimmery 0:13d8a77fb1d7 9 PwmOut right_forward(PB_10);
jimmery 0:13d8a77fb1d7 10 PwmOut right_reverse(PA_6);
jimmery 0:13d8a77fb1d7 11 PwmOut left_forward(PA_7);
jimmery 0:13d8a77fb1d7 12 PwmOut left_reverse(PB_6);
jimmery 0:13d8a77fb1d7 13
jimmery 0:13d8a77fb1d7 14 // TODO: change our encoder pins from AnalogIn into:
jimmery 0:13d8a77fb1d7 15 // otherwise, we can also use the AVEncoder thing as well.
jimmery 0:13d8a77fb1d7 16 AVEncoder l_enco(PA_15, PB_3);
jimmery 0:13d8a77fb1d7 17 AVEncoder r_enco(PA_1, PA_10);
jimmery 0:13d8a77fb1d7 18
jimmery 0:13d8a77fb1d7 19 // gyro
jimmery 0:13d8a77fb1d7 20 AnalogIn _gyro(PA_0);
jimmery 0:13d8a77fb1d7 21 // AnalogIn gyro_cal(PC_1) ?? currently this isn't connected.
jimmery 0:13d8a77fb1d7 22
jimmery 0:13d8a77fb1d7 23 //Left Front IR
jimmery 0:13d8a77fb1d7 24 DigitalOut eLF(PC_3);
jimmery 0:13d8a77fb1d7 25 AnalogIn rLF(PC_0);
jimmery 0:13d8a77fb1d7 26 //PC_4 is an ADC
jimmery 0:13d8a77fb1d7 27 //Left Side IR
jimmery 0:13d8a77fb1d7 28 DigitalOut eLS(PC_2);
jimmery 0:13d8a77fb1d7 29 AnalogIn rLS(PC_1);
jimmery 0:13d8a77fb1d7 30
jimmery 0:13d8a77fb1d7 31 //Right Front IR
jimmery 0:13d8a77fb1d7 32 DigitalOut eRF(PC_12);
jimmery 0:13d8a77fb1d7 33 AnalogIn rRF(PA_4);
jimmery 0:13d8a77fb1d7 34
jimmery 0:13d8a77fb1d7 35 //Right Side IR
jimmery 0:13d8a77fb1d7 36 DigitalOut eRS(PC_15);
jimmery 0:13d8a77fb1d7 37 AnalogIn rRS(PB_0);
jimmery 0:13d8a77fb1d7 38
jimmery 0:13d8a77fb1d7 39 DigitalOut myled(LED1);
jimmery 0:13d8a77fb1d7 40
jimmery 0:13d8a77fb1d7 41 volatile float gyro_offset = 0;
jimmery 0:13d8a77fb1d7 42
jimmery 0:13d8a77fb1d7 43 volatile float line_prevError = 0;
jimmery 0:13d8a77fb1d7 44 volatile float enco_prevError = 0;
jimmery 0:13d8a77fb1d7 45 volatile float gyro_prevError = 0;
jimmery 0:13d8a77fb1d7 46
jimmery 0:13d8a77fb1d7 47 volatile float line_accumulator = 0;
jimmery 0:13d8a77fb1d7 48 volatile float line_decayFactor = 1;
jimmery 0:13d8a77fb1d7 49 volatile float enco_accumulator = 0;
jimmery 0:13d8a77fb1d7 50 volatile float enco_decayFactor = 1;
jimmery 0:13d8a77fb1d7 51 volatile float gyro_accumulator = 0;
jimmery 0:13d8a77fb1d7 52 volatile float gyro_decayFactor = 1;
jimmery 0:13d8a77fb1d7 53
jimmery 0:13d8a77fb1d7 54 volatile float left_speed = 0.5;
jimmery 0:13d8a77fb1d7 55 volatile float right_speed = 0.5;
jimmery 0:13d8a77fb1d7 56
jimmery 0:13d8a77fb1d7 57 const float left_max_speed = 6; // max speed is 6 encoder pulses per ms.
jimmery 0:13d8a77fb1d7 58 const float right_max_speed = 6.2;
jimmery 0:13d8a77fb1d7 59
jimmery 0:13d8a77fb1d7 60 const float gyro_propo = 0.75;
jimmery 0:13d8a77fb1d7 61 const float gyro_integ = 0;
jimmery 0:13d8a77fb1d7 62 const float gyro_deriv = 1;
jimmery 0:13d8a77fb1d7 63
jimmery 0:13d8a77fb1d7 64 const float enco_propo = 0.05;
jimmery 0:13d8a77fb1d7 65 const float enco_integ = 0;
jimmery 0:13d8a77fb1d7 66 const float enco_deriv = 5;
jimmery 0:13d8a77fb1d7 67
jimmery 0:13d8a77fb1d7 68 const float spin_enco_weight = 0.5;
jimmery 0:13d8a77fb1d7 69 const float spin_gyro_weight = 1 - spin_enco_weight;
jimmery 0:13d8a77fb1d7 70
jimmery 0:13d8a77fb1d7 71 volatile float enco_error;
jimmery 0:13d8a77fb1d7 72 volatile float enco_pid;
jimmery 0:13d8a77fb1d7 73 volatile float gyro_error;
jimmery 0:13d8a77fb1d7 74 volatile float gyro_pid;
jimmery 0:13d8a77fb1d7 75 volatile float w_error;
jimmery 0:13d8a77fb1d7 76
jimmery 0:13d8a77fb1d7 77 void reset()
jimmery 0:13d8a77fb1d7 78 {
jimmery 0:13d8a77fb1d7 79 l_enco.reset();
jimmery 0:13d8a77fb1d7 80 r_enco.reset();
jimmery 0:13d8a77fb1d7 81 }
jimmery 0:13d8a77fb1d7 82
jimmery 0:13d8a77fb1d7 83 void systick()
jimmery 0:13d8a77fb1d7 84 {
jimmery 0:13d8a77fb1d7 85 enco_error = l_enco.getPulses() - r_enco.getPulses();
jimmery 0:13d8a77fb1d7 86 gyro_error = _gyro.read() - gyro_offset;
jimmery 0:13d8a77fb1d7 87
jimmery 0:13d8a77fb1d7 88 enco_pid = 0;
jimmery 0:13d8a77fb1d7 89 //enco_pid += enco_propo * enco_error;
jimmery 0:13d8a77fb1d7 90 //enco_pid += enco_deriv * (enco_error - enco_prevError);
jimmery 0:13d8a77fb1d7 91
jimmery 0:13d8a77fb1d7 92 gyro_pid = 0;
jimmery 0:13d8a77fb1d7 93 gyro_pid += gyro_propo * gyro_error;
jimmery 0:13d8a77fb1d7 94 gyro_pid += gyro_deriv * (gyro_error - gyro_prevError);
jimmery 0:13d8a77fb1d7 95
jimmery 0:13d8a77fb1d7 96 w_error = spin_enco_weight * enco_pid + spin_gyro_weight * gyro_pid;
jimmery 0:13d8a77fb1d7 97 left_speed += w_error;
jimmery 0:13d8a77fb1d7 98 right_speed -= w_error;
jimmery 0:13d8a77fb1d7 99
jimmery 0:13d8a77fb1d7 100 left_forward = left_speed / left_max_speed;
jimmery 0:13d8a77fb1d7 101 left_reverse = 0;
jimmery 0:13d8a77fb1d7 102 right_forward = right_speed / right_max_speed;
jimmery 0:13d8a77fb1d7 103 right_reverse = 0;
jimmery 0:13d8a77fb1d7 104
jimmery 0:13d8a77fb1d7 105 reset();
jimmery 0:13d8a77fb1d7 106 }
jimmery 0:13d8a77fb1d7 107
jimmery 0:13d8a77fb1d7 108 // computes gyro_offset
jimmery 0:13d8a77fb1d7 109 // uses a "weighted" average.
jimmery 0:13d8a77fb1d7 110 // idea is that the current gyro offset is weighted more than previous ones.
jimmery 0:13d8a77fb1d7 111 // uses the following y(n) = 1/2 * y(n-1) + 1/2 * x(n).
jimmery 0:13d8a77fb1d7 112 // (therefore y(n) = sum of x(i)/2^i from i from 0 to n.)
jimmery 0:13d8a77fb1d7 113 // this maintains that there will be some influence from previous factors, but keeps the current value at a higher weight.
jimmery 0:13d8a77fb1d7 114 // currently this is only in the setup function. we can run this when the mouse is running in a line
jimmery 0:13d8a77fb1d7 115 // when we figure out good line running pid.
jimmery 0:13d8a77fb1d7 116 void offsetCalc()
jimmery 0:13d8a77fb1d7 117 {
jimmery 0:13d8a77fb1d7 118 gyro_offset = gyro_offset / 2 + _gyro.read() / 2;
jimmery 0:13d8a77fb1d7 119 }
jimmery 0:13d8a77fb1d7 120
jimmery 0:13d8a77fb1d7 121
jimmery 0:13d8a77fb1d7 122 void setup()
jimmery 0:13d8a77fb1d7 123 {
jimmery 0:13d8a77fb1d7 124 pc.printf("Hello World\r\n");
jimmery 0:13d8a77fb1d7 125
jimmery 0:13d8a77fb1d7 126 eRS = 0;
jimmery 0:13d8a77fb1d7 127 eRF = 0;
jimmery 0:13d8a77fb1d7 128 eLS = 0;
jimmery 0:13d8a77fb1d7 129 eLF = 0;
jimmery 0:13d8a77fb1d7 130
jimmery 0:13d8a77fb1d7 131 myled = 1;
jimmery 0:13d8a77fb1d7 132
jimmery 0:13d8a77fb1d7 133 for ( int i = 0; i < 1000; i++ )
jimmery 0:13d8a77fb1d7 134 {
jimmery 0:13d8a77fb1d7 135 offsetCalc();
jimmery 0:13d8a77fb1d7 136 }
jimmery 0:13d8a77fb1d7 137
jimmery 0:13d8a77fb1d7 138 //left_forward = left_speed / left_max_speed;
jimmery 0:13d8a77fb1d7 139 // left_reverse = 0;
jimmery 0:13d8a77fb1d7 140 // right_forward = right_speed / right_max_speed;
jimmery 0:13d8a77fb1d7 141 // right_reverse = 0;
jimmery 0:13d8a77fb1d7 142
jimmery 0:13d8a77fb1d7 143 wait(2);
jimmery 0:13d8a77fb1d7 144
jimmery 0:13d8a77fb1d7 145 // repeat this for some time frame.
jimmery 0:13d8a77fb1d7 146 Systicker.attach_us(&systick, 1000);
jimmery 0:13d8a77fb1d7 147 }
jimmery 0:13d8a77fb1d7 148
jimmery 0:13d8a77fb1d7 149
jimmery 0:13d8a77fb1d7 150 int main() {
jimmery 0:13d8a77fb1d7 151 setup();
jimmery 0:13d8a77fb1d7 152 while(1) {
jimmery 0:13d8a77fb1d7 153
jimmery 0:13d8a77fb1d7 154 pc.printf("enco_error: %f, gyro_error: %f, w_error: %f\r\n", enco_error, gyro_error, w_error);
jimmery 0:13d8a77fb1d7 155 //wait(1);
jimmery 0:13d8a77fb1d7 156 }
jimmery 0:13d8a77fb1d7 157 }