self-balancing-robot
Dependencies: mbed mbed-rtos Motor
main.cpp@24:105abc3a7c72, 2020-04-23 (annotated)
- Committer:
- pandirimukund
- Date:
- Thu Apr 23 16:36:53 2020 +0000
- Revision:
- 24:105abc3a7c72
- Parent:
- 22:bb9b9d3b45d8
added initial speed stuff
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 1:491820ee784d | 1 | #include "mbed.h" |
mbed_official | 11:0309bef74ba8 | 2 | #include "rtos.h" |
pandirimukund | 13:8d8ac3189984 | 3 | #include "LSM9DS1.h" |
pandirimukund | 12:1fc4b3d94397 | 4 | |
pandirimukund | 12:1fc4b3d94397 | 5 | |
pandirimukund | 12:1fc4b3d94397 | 6 | /////////////////////////////////////////////////////////////////////////////// |
pandirimukund | 12:1fc4b3d94397 | 7 | ///////////////////////////// Variable Initialization////////////////////////// |
pandirimukund | 12:1fc4b3d94397 | 8 | /////////////////////////////////////////////////////////////////////////////// |
pandirimukund | 12:1fc4b3d94397 | 9 | Ticker bluetooth; |
pandirimukund | 12:1fc4b3d94397 | 10 | Serial pc(USBTX, USBRX); |
pandirimukund | 12:1fc4b3d94397 | 11 | |
pandirimukund | 13:8d8ac3189984 | 12 | |
pandirimukund | 12:1fc4b3d94397 | 13 | Mutex parametersmutex; |
pandirimukund | 15:f0f19572c4a5 | 14 | Mutex angleMutex; |
pandirimukund | 12:1fc4b3d94397 | 15 | Serial blue(p28, p27); |
pandirimukund | 12:1fc4b3d94397 | 16 | |
pandirimukund | 13:8d8ac3189984 | 17 | LSM9DS1 imu(p9, p10, 0xD6, 0x3C); |
pandirimukund | 12:1fc4b3d94397 | 18 | |
pandirimukund | 24:105abc3a7c72 | 19 | PwmOut leftWheel(p21); |
pandirimukund | 24:105abc3a7c72 | 20 | PwmOut rightWheel(p22); |
pandirimukund | 24:105abc3a7c72 | 21 | |
pandirimukund | 12:1fc4b3d94397 | 22 | |
pandirimukund | 12:1fc4b3d94397 | 23 | /////////////////////////////////////////////////////////////////////////////// |
pandirimukund | 12:1fc4b3d94397 | 24 | ///////////////////////////// Control System Variables///////////////////////// |
pandirimukund | 12:1fc4b3d94397 | 25 | /////////////////////////////////////////////////////////////////////////////// |
pandirimukund | 12:1fc4b3d94397 | 26 | float rp = 50; |
pandirimukund | 12:1fc4b3d94397 | 27 | float rd = 51; |
pandirimukund | 12:1fc4b3d94397 | 28 | float ri = 50; |
pandirimukund | 12:1fc4b3d94397 | 29 | float desired_angle = 0; |
pandirimukund | 12:1fc4b3d94397 | 30 | |
pandirimukund | 12:1fc4b3d94397 | 31 | float speed = 0; |
pandirimukund | 12:1fc4b3d94397 | 32 | |
pandirimukund | 12:1fc4b3d94397 | 33 | float pAngle = 0; |
pandirimukund | 12:1fc4b3d94397 | 34 | float dAngle = 0; |
pandirimukund | 12:1fc4b3d94397 | 35 | float iAngle = 0; |
pandirimukund | 12:1fc4b3d94397 | 36 | |
pandirimukund | 12:1fc4b3d94397 | 37 | int time_segment = 5; //Update the speed every 5 milliseconds |
pandirimukund | 12:1fc4b3d94397 | 38 | |
lrucker7 | 16:f9e3df933304 | 39 | void get_current_angle(); |
pandirimukund | 12:1fc4b3d94397 | 40 | |
pandirimukund | 18:11883c581785 | 41 | float angleBiasGyro = 0; |
pandirimukund | 19:9ea181b003af | 42 | float angleBiasAcc = 0; |
pandirimukund | 17:afde478daa01 | 43 | |
pandirimukund | 12:1fc4b3d94397 | 44 | |
pandirimukund | 12:1fc4b3d94397 | 45 | /////////////////////////////////////////////////////////////////////////////// |
pandirimukund | 12:1fc4b3d94397 | 46 | ///////////////////////////// Bluetooth Section /////////////////////////////// |
pandirimukund | 12:1fc4b3d94397 | 47 | /////////////////////////////////////////////////////////////////////////////// |
pandirimukund | 12:1fc4b3d94397 | 48 | void bluetooth_update() { |
pandirimukund | 12:1fc4b3d94397 | 49 | char bnum = 0; |
pandirimukund | 12:1fc4b3d94397 | 50 | char bhit = 0; |
pandirimukund | 12:1fc4b3d94397 | 51 | while (1) { |
pandirimukund | 12:1fc4b3d94397 | 52 | if (blue.getc() == '!') { |
pandirimukund | 12:1fc4b3d94397 | 53 | if (blue.getc() == 'B') { //button data packet |
pandirimukund | 12:1fc4b3d94397 | 54 | bnum = blue.getc(); //button number |
pandirimukund | 12:1fc4b3d94397 | 55 | //pc.printf("%d",bnum); |
pandirimukund | 12:1fc4b3d94397 | 56 | bhit = blue.getc(); //1=hit, 0=release |
pandirimukund | 12:1fc4b3d94397 | 57 | parametersmutex.lock(); |
pandirimukund | 12:1fc4b3d94397 | 58 | if (blue.getc() == char(~('!' + 'B' + bnum + bhit))) { //checksum OK? |
pandirimukund | 12:1fc4b3d94397 | 59 | switch (bnum) { |
pandirimukund | 12:1fc4b3d94397 | 60 | case '1': //number button 1 |
pandirimukund | 12:1fc4b3d94397 | 61 | if (bhit == '1') { |
pandirimukund | 12:1fc4b3d94397 | 62 | rd += 1; |
pandirimukund | 12:1fc4b3d94397 | 63 | } else { |
pandirimukund | 12:1fc4b3d94397 | 64 | //add release code here |
pandirimukund | 12:1fc4b3d94397 | 65 | } |
pandirimukund | 12:1fc4b3d94397 | 66 | break; |
pandirimukund | 12:1fc4b3d94397 | 67 | case '2': //number button 2 |
pandirimukund | 12:1fc4b3d94397 | 68 | if (bhit == '1') { |
pandirimukund | 12:1fc4b3d94397 | 69 | ri += 1; |
pandirimukund | 12:1fc4b3d94397 | 70 | } else { |
pandirimukund | 12:1fc4b3d94397 | 71 | //add release code here |
pandirimukund | 12:1fc4b3d94397 | 72 | } |
pandirimukund | 12:1fc4b3d94397 | 73 | break; |
pandirimukund | 12:1fc4b3d94397 | 74 | case '3': //number button 3 |
pandirimukund | 12:1fc4b3d94397 | 75 | if (bhit == '1') { |
pandirimukund | 12:1fc4b3d94397 | 76 | rd -= 1; |
pandirimukund | 12:1fc4b3d94397 | 77 | } else { |
pandirimukund | 12:1fc4b3d94397 | 78 | //add release code here |
pandirimukund | 12:1fc4b3d94397 | 79 | } |
pandirimukund | 12:1fc4b3d94397 | 80 | break; |
pandirimukund | 12:1fc4b3d94397 | 81 | case '4': //number button 4 |
pandirimukund | 12:1fc4b3d94397 | 82 | if (bhit == '1') { |
pandirimukund | 12:1fc4b3d94397 | 83 | ri -= 1; |
pandirimukund | 12:1fc4b3d94397 | 84 | } else { |
pandirimukund | 12:1fc4b3d94397 | 85 | //add release code here |
pandirimukund | 12:1fc4b3d94397 | 86 | } |
pandirimukund | 12:1fc4b3d94397 | 87 | break; |
pandirimukund | 12:1fc4b3d94397 | 88 | case '5': //button 5 up arrow |
pandirimukund | 12:1fc4b3d94397 | 89 | if (bhit == '1') { |
pandirimukund | 12:1fc4b3d94397 | 90 | rp += 1; |
pandirimukund | 12:1fc4b3d94397 | 91 | } else { |
pandirimukund | 12:1fc4b3d94397 | 92 | //add release code here |
pandirimukund | 12:1fc4b3d94397 | 93 | } |
pandirimukund | 12:1fc4b3d94397 | 94 | break; |
pandirimukund | 12:1fc4b3d94397 | 95 | case '6': //button 6 down arrow |
pandirimukund | 12:1fc4b3d94397 | 96 | if (bhit == '1') { |
pandirimukund | 12:1fc4b3d94397 | 97 | rp -= 1; |
pandirimukund | 12:1fc4b3d94397 | 98 | } else { |
pandirimukund | 12:1fc4b3d94397 | 99 | //add release code here |
pandirimukund | 12:1fc4b3d94397 | 100 | } |
pandirimukund | 12:1fc4b3d94397 | 101 | break; |
pandirimukund | 12:1fc4b3d94397 | 102 | case '7': //button 7 left arrow |
pandirimukund | 12:1fc4b3d94397 | 103 | if (bhit == '1') { |
pandirimukund | 12:1fc4b3d94397 | 104 | desired_angle -= 1; |
pandirimukund | 12:1fc4b3d94397 | 105 | } else { |
pandirimukund | 12:1fc4b3d94397 | 106 | //add release code here |
pandirimukund | 12:1fc4b3d94397 | 107 | } |
pandirimukund | 12:1fc4b3d94397 | 108 | break; |
pandirimukund | 12:1fc4b3d94397 | 109 | case '8': //button 8 right arrow |
pandirimukund | 12:1fc4b3d94397 | 110 | if (bhit == '1') { |
pandirimukund | 12:1fc4b3d94397 | 111 | desired_angle += 1; |
pandirimukund | 12:1fc4b3d94397 | 112 | } else { |
pandirimukund | 12:1fc4b3d94397 | 113 | //add release code here |
pandirimukund | 12:1fc4b3d94397 | 114 | } |
pandirimukund | 12:1fc4b3d94397 | 115 | break; |
pandirimukund | 12:1fc4b3d94397 | 116 | default: |
pandirimukund | 12:1fc4b3d94397 | 117 | break; |
pandirimukund | 12:1fc4b3d94397 | 118 | } |
pandirimukund | 12:1fc4b3d94397 | 119 | } |
pandirimukund | 12:1fc4b3d94397 | 120 | parametersmutex.unlock(); |
pandirimukund | 12:1fc4b3d94397 | 121 | } |
pandirimukund | 12:1fc4b3d94397 | 122 | } |
pandirimukund | 12:1fc4b3d94397 | 123 | Thread::wait(100); |
emilmont | 1:491820ee784d | 124 | } |
emilmont | 1:491820ee784d | 125 | } |
pandirimukund | 12:1fc4b3d94397 | 126 | |
pandirimukund | 24:105abc3a7c72 | 127 | /////////////////////////////////////////////////////////////////////////////// |
pandirimukund | 24:105abc3a7c72 | 128 | ///////////////////////////// update motor speeds/////////////////////////// |
pandirimukund | 24:105abc3a7c72 | 129 | /////////////////////////////////////////////////////////////////////////////// |
pandirimukund | 24:105abc3a7c72 | 130 | void set_wheel_speed(float speed) { |
pandirimukund | 24:105abc3a7c72 | 131 | if(speed > 1) speed = 1; |
pandirimukund | 24:105abc3a7c72 | 132 | if speed < -1) speed = -1; |
pandirimukund | 24:105abc3a7c72 | 133 | leftWheel = speed; |
pandirimukund | 24:105abc3a7c72 | 134 | rightWheel = speed; |
pandirimukund | 24:105abc3a7c72 | 135 | } |
pandirimukund | 24:105abc3a7c72 | 136 | |
pandirimukund | 12:1fc4b3d94397 | 137 | |
pandirimukund | 12:1fc4b3d94397 | 138 | /////////////////////////////////////////////////////////////////////////////// |
pandirimukund | 12:1fc4b3d94397 | 139 | ///////////////////////////// Control System Updates/////////////////////////// |
pandirimukund | 12:1fc4b3d94397 | 140 | /////////////////////////////////////////////////////////////////////////////// |
pandirimukund | 12:1fc4b3d94397 | 141 | //make the calls to IMU here and this should be another thread |
pandirimukund | 12:1fc4b3d94397 | 142 | void update_system() { |
lrucker7 | 16:f9e3df933304 | 143 | while(1){ |
lrucker7 | 16:f9e3df933304 | 144 | get_current_angle(); |
lrucker7 | 16:f9e3df933304 | 145 | //pc.printf("this is running 100"); |
pandirimukund | 24:105abc3a7c72 | 146 | angleMutex.lock(); |
pandirimukund | 24:105abc3a7c72 | 147 | speed = -1*(rp*pAngle + ri*iAngle + rd*dAngle)/150; |
pandirimukund | 24:105abc3a7c72 | 148 | set_wheel_speed(speed); |
pandirimukund | 24:105abc3a7c72 | 149 | angleMutex.unlock(); |
lrucker7 | 16:f9e3df933304 | 150 | Thread::wait(10); |
lrucker7 | 16:f9e3df933304 | 151 | } |
pandirimukund | 12:1fc4b3d94397 | 152 | } |
pandirimukund | 12:1fc4b3d94397 | 153 | |
pandirimukund | 12:1fc4b3d94397 | 154 | |
pandirimukund | 12:1fc4b3d94397 | 155 | /////////////////////////////////////////////////////////////////////////////// |
pandirimukund | 12:1fc4b3d94397 | 156 | ///////////////////////////// IMU STUFF//////////////////////////////////////// |
pandirimukund | 12:1fc4b3d94397 | 157 | /////////////////////////////////////////////////////////////////////////////// |
pandirimukund | 12:1fc4b3d94397 | 158 | void calibrate_imu() { |
pandirimukund | 17:afde478daa01 | 159 | for(int i = 0; i < 500; i++){ |
pandirimukund | 17:afde478daa01 | 160 | imu.readGyro(); |
pandirimukund | 19:9ea181b003af | 161 | angleBiasGyro += imu.calcGyro(imu.gy); |
lrucker7 | 20:6b5f83412413 | 162 | //angleBiasAcc += (-1)*atan2(imu.ax,imu.az)*180/3.142-90 |
pandirimukund | 17:afde478daa01 | 163 | } |
pandirimukund | 18:11883c581785 | 164 | angleBiasGyro /= 500; |
pandirimukund | 18:11883c581785 | 165 | angleBiasAcc /= 500; |
pandirimukund | 12:1fc4b3d94397 | 166 | } |
pandirimukund | 12:1fc4b3d94397 | 167 | |
pandirimukund | 12:1fc4b3d94397 | 168 | void get_current_angle() { |
pandirimukund | 13:8d8ac3189984 | 169 | // return; |
pandirimukund | 13:8d8ac3189984 | 170 | imu.readGyro(); |
pandirimukund | 22:bb9b9d3b45d8 | 171 | int gyro = -(imu.calcGyro(imu.gy)) * .01; |
lrucker7 | 20:6b5f83412413 | 172 | //int acc = (-1)*atan2(imu.ax,imu.az)*180/3.142-90-angleBiasAcc; |
lrucker7 | 16:f9e3df933304 | 173 | //pc.printf("gyro:%f",gyro); |
pandirimukund | 15:f0f19572c4a5 | 174 | angleMutex.lock(); |
pandirimukund | 24:105abc3a7c72 | 175 | dAngle = pAngle; |
pandirimukund | 24:105abc3a7c72 | 176 | pAngle += 1*gyro; //+ 0.02*acc; |
pandirimukund | 24:105abc3a7c72 | 177 | dAngle = pAngle - dAngle; |
pandirimukund | 24:105abc3a7c72 | 178 | iAngle += pAngle * 0.01; |
pandirimukund | 15:f0f19572c4a5 | 179 | angleMutex.unlock(); |
pandirimukund | 12:1fc4b3d94397 | 180 | } |
pandirimukund | 12:1fc4b3d94397 | 181 | |
pandirimukund | 12:1fc4b3d94397 | 182 | |
pandirimukund | 12:1fc4b3d94397 | 183 | |
pandirimukund | 12:1fc4b3d94397 | 184 | |
pandirimukund | 12:1fc4b3d94397 | 185 | /////////////////////////////////////////////////////////////////////////////// |
pandirimukund | 12:1fc4b3d94397 | 186 | ///////////////////////////// Running Main Function//////////////////////////// |
pandirimukund | 12:1fc4b3d94397 | 187 | /////////////////////////////////////////////////////////////////////////////// |
pandirimukund | 12:1fc4b3d94397 | 188 | |
emilmont | 1:491820ee784d | 189 | int main() { |
pandirimukund | 12:1fc4b3d94397 | 190 | pc.printf("this is running"); |
lrucker7 | 21:949558e7a48c | 191 | |
pandirimukund | 12:1fc4b3d94397 | 192 | Thread bluetooth; |
pandirimukund | 12:1fc4b3d94397 | 193 | Thread system_update; |
emilmont | 1:491820ee784d | 194 | |
pandirimukund | 14:f9c2cf6643cf | 195 | imu.begin(); |
pandirimukund | 22:bb9b9d3b45d8 | 196 | imu.calibrate(1); |
pandirimukund | 22:bb9b9d3b45d8 | 197 | |
pandirimukund | 12:1fc4b3d94397 | 198 | bluetooth.start(bluetooth_update); |
pandirimukund | 12:1fc4b3d94397 | 199 | system_update.start(update_system); |
pandirimukund | 12:1fc4b3d94397 | 200 | |
pandirimukund | 12:1fc4b3d94397 | 201 | //bluetooth.attach(&bluetooth_update, 0.1); |
pandirimukund | 12:1fc4b3d94397 | 202 | while (1) { |
lrucker7 | 16:f9e3df933304 | 203 | |
pandirimukund | 12:1fc4b3d94397 | 204 | //bluetooth_update(); |
lrucker7 | 16:f9e3df933304 | 205 | //parametersmutex.lock(); |
lrucker7 | 16:f9e3df933304 | 206 | // pc.printf("rp: %f, rd: %f, ri: %f, desired_angle: %f\n", rp, rd, ri, desired_angle); |
lrucker7 | 16:f9e3df933304 | 207 | // parametersmutex.unlock(); |
pandirimukund | 12:1fc4b3d94397 | 208 | |
pandirimukund | 15:f0f19572c4a5 | 209 | angleMutex.lock(); |
pandirimukund | 15:f0f19572c4a5 | 210 | pc.printf("pAngle: %f", pAngle); |
pandirimukund | 24:105abc3a7c72 | 211 | pc.printf("speed: %f", speed); |
pandirimukund | 15:f0f19572c4a5 | 212 | angleMutex.unlock(); |
pandirimukund | 15:f0f19572c4a5 | 213 | |
pandirimukund | 15:f0f19572c4a5 | 214 | |
lrucker7 | 16:f9e3df933304 | 215 | //get_current_angle(); |
pandirimukund | 14:f9c2cf6643cf | 216 | |
pandirimukund | 12:1fc4b3d94397 | 217 | |
pandirimukund | 12:1fc4b3d94397 | 218 | Thread::wait(100); |
emilmont | 1:491820ee784d | 219 | } |
emilmont | 1:491820ee784d | 220 | } |
pandirimukund | 12:1fc4b3d94397 | 221 |