self-balancing-robot

Dependencies:   mbed mbed-rtos Motor

Committer:
pandirimukund
Date:
Thu Apr 23 19:31:24 2020 +0000
Revision:
27:d053966e9320
Parent:
26:dcf173d2904f
Child:
28:31ae3dd720e1
updated angles and accounted for desired angle;

Who changed what in which revision?

UserRevisionLine numberNew 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 12:1fc4b3d94397 19
pandirimukund 12:1fc4b3d94397 20 ///////////////////////////////////////////////////////////////////////////////
pandirimukund 12:1fc4b3d94397 21 ///////////////////////////// Control System Variables/////////////////////////
pandirimukund 12:1fc4b3d94397 22 ///////////////////////////////////////////////////////////////////////////////
pandirimukund 12:1fc4b3d94397 23 float rp = 50;
pandirimukund 12:1fc4b3d94397 24 float rd = 51;
pandirimukund 12:1fc4b3d94397 25 float ri = 50;
pandirimukund 12:1fc4b3d94397 26 float desired_angle = 0;
pandirimukund 12:1fc4b3d94397 27
pandirimukund 12:1fc4b3d94397 28 float speed = 0;
pandirimukund 12:1fc4b3d94397 29
pandirimukund 12:1fc4b3d94397 30 float pAngle = 0;
pandirimukund 12:1fc4b3d94397 31 float dAngle = 0;
pandirimukund 12:1fc4b3d94397 32 float iAngle = 0;
pandirimukund 12:1fc4b3d94397 33
pandirimukund 12:1fc4b3d94397 34 int time_segment = 5; //Update the speed every 5 milliseconds
pandirimukund 12:1fc4b3d94397 35
lrucker7 16:f9e3df933304 36 void get_current_angle();
pandirimukund 12:1fc4b3d94397 37
pandirimukund 17:afde478daa01 38 float angleBias = 0;
pandirimukund 17:afde478daa01 39
pandirimukund 26:dcf173d2904f 40 PwmOut leftWheel(p21);
pandirimukund 26:dcf173d2904f 41 PwmOut rightWheel(p22);
pandirimukund 26:dcf173d2904f 42
pandirimukund 12:1fc4b3d94397 43
pandirimukund 12:1fc4b3d94397 44 ///////////////////////////////////////////////////////////////////////////////
pandirimukund 12:1fc4b3d94397 45 ///////////////////////////// Bluetooth Section ///////////////////////////////
pandirimukund 12:1fc4b3d94397 46 ///////////////////////////////////////////////////////////////////////////////
pandirimukund 12:1fc4b3d94397 47 void bluetooth_update() {
pandirimukund 12:1fc4b3d94397 48 char bnum = 0;
pandirimukund 12:1fc4b3d94397 49 char bhit = 0;
pandirimukund 12:1fc4b3d94397 50 while (1) {
pandirimukund 12:1fc4b3d94397 51 if (blue.getc() == '!') {
pandirimukund 12:1fc4b3d94397 52 if (blue.getc() == 'B') { //button data packet
pandirimukund 12:1fc4b3d94397 53 bnum = blue.getc(); //button number
pandirimukund 12:1fc4b3d94397 54 //pc.printf("%d",bnum);
pandirimukund 12:1fc4b3d94397 55 bhit = blue.getc(); //1=hit, 0=release
pandirimukund 12:1fc4b3d94397 56 parametersmutex.lock();
pandirimukund 12:1fc4b3d94397 57 if (blue.getc() == char(~('!' + 'B' + bnum + bhit))) { //checksum OK?
pandirimukund 12:1fc4b3d94397 58 switch (bnum) {
pandirimukund 12:1fc4b3d94397 59 case '1': //number button 1
pandirimukund 12:1fc4b3d94397 60 if (bhit == '1') {
pandirimukund 12:1fc4b3d94397 61 rd += 1;
pandirimukund 12:1fc4b3d94397 62 } else {
pandirimukund 12:1fc4b3d94397 63 //add release code here
pandirimukund 12:1fc4b3d94397 64 }
pandirimukund 12:1fc4b3d94397 65 break;
pandirimukund 12:1fc4b3d94397 66 case '2': //number button 2
pandirimukund 12:1fc4b3d94397 67 if (bhit == '1') {
pandirimukund 12:1fc4b3d94397 68 ri += 1;
pandirimukund 12:1fc4b3d94397 69 } else {
pandirimukund 12:1fc4b3d94397 70 //add release code here
pandirimukund 12:1fc4b3d94397 71 }
pandirimukund 12:1fc4b3d94397 72 break;
pandirimukund 12:1fc4b3d94397 73 case '3': //number button 3
pandirimukund 12:1fc4b3d94397 74 if (bhit == '1') {
pandirimukund 12:1fc4b3d94397 75 rd -= 1;
pandirimukund 12:1fc4b3d94397 76 } else {
pandirimukund 12:1fc4b3d94397 77 //add release code here
pandirimukund 12:1fc4b3d94397 78 }
pandirimukund 12:1fc4b3d94397 79 break;
pandirimukund 12:1fc4b3d94397 80 case '4': //number button 4
pandirimukund 12:1fc4b3d94397 81 if (bhit == '1') {
pandirimukund 12:1fc4b3d94397 82 ri -= 1;
pandirimukund 12:1fc4b3d94397 83 } else {
pandirimukund 12:1fc4b3d94397 84 //add release code here
pandirimukund 12:1fc4b3d94397 85 }
pandirimukund 12:1fc4b3d94397 86 break;
pandirimukund 12:1fc4b3d94397 87 case '5': //button 5 up arrow
pandirimukund 12:1fc4b3d94397 88 if (bhit == '1') {
pandirimukund 12:1fc4b3d94397 89 rp += 1;
pandirimukund 12:1fc4b3d94397 90 } else {
pandirimukund 12:1fc4b3d94397 91 //add release code here
pandirimukund 12:1fc4b3d94397 92 }
pandirimukund 12:1fc4b3d94397 93 break;
pandirimukund 12:1fc4b3d94397 94 case '6': //button 6 down arrow
pandirimukund 12:1fc4b3d94397 95 if (bhit == '1') {
pandirimukund 12:1fc4b3d94397 96 rp -= 1;
pandirimukund 12:1fc4b3d94397 97 } else {
pandirimukund 12:1fc4b3d94397 98 //add release code here
pandirimukund 12:1fc4b3d94397 99 }
pandirimukund 12:1fc4b3d94397 100 break;
pandirimukund 12:1fc4b3d94397 101 case '7': //button 7 left arrow
pandirimukund 12:1fc4b3d94397 102 if (bhit == '1') {
pandirimukund 12:1fc4b3d94397 103 desired_angle -= 1;
pandirimukund 12:1fc4b3d94397 104 } else {
pandirimukund 12:1fc4b3d94397 105 //add release code here
pandirimukund 12:1fc4b3d94397 106 }
pandirimukund 12:1fc4b3d94397 107 break;
pandirimukund 12:1fc4b3d94397 108 case '8': //button 8 right arrow
pandirimukund 12:1fc4b3d94397 109 if (bhit == '1') {
pandirimukund 12:1fc4b3d94397 110 desired_angle += 1;
pandirimukund 12:1fc4b3d94397 111 } else {
pandirimukund 12:1fc4b3d94397 112 //add release code here
pandirimukund 12:1fc4b3d94397 113 }
pandirimukund 12:1fc4b3d94397 114 break;
pandirimukund 12:1fc4b3d94397 115 default:
pandirimukund 12:1fc4b3d94397 116 break;
pandirimukund 12:1fc4b3d94397 117 }
pandirimukund 12:1fc4b3d94397 118 }
pandirimukund 12:1fc4b3d94397 119 parametersmutex.unlock();
pandirimukund 12:1fc4b3d94397 120 }
pandirimukund 12:1fc4b3d94397 121 }
pandirimukund 12:1fc4b3d94397 122 Thread::wait(100);
emilmont 1:491820ee784d 123 }
emilmont 1:491820ee784d 124 }
pandirimukund 12:1fc4b3d94397 125
pandirimukund 25:154c74800ade 126 ///////////////////////////////////////////////////////////////////////////////
pandirimukund 25:154c74800ade 127 ///////////////////////////// update motor speeds///////////////////////////
pandirimukund 25:154c74800ade 128 ///////////////////////////////////////////////////////////////////////////////
pandirimukund 25:154c74800ade 129 void set_wheel_speed(float speed) {
pandirimukund 25:154c74800ade 130 if(speed > 1) speed = 1;
pandirimukund 25:154c74800ade 131 if (speed < -1) speed = -1;
pandirimukund 25:154c74800ade 132 leftWheel = speed;
pandirimukund 25:154c74800ade 133 rightWheel = speed;
pandirimukund 25:154c74800ade 134 }
pandirimukund 25:154c74800ade 135
pandirimukund 25:154c74800ade 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();
pandirimukund 25:154c74800ade 145
pandirimukund 26:dcf173d2904f 146 angleMutex.lock();
pandirimukund 27:d053966e9320 147 speed = -1*(rp*pAngle + ri*iAngle + rd*dAngle)/(70 * 150 * 3);
pandirimukund 26:dcf173d2904f 148 set_wheel_speed(speed);
pandirimukund 26:dcf173d2904f 149 angleMutex.unlock();
pandirimukund 26:dcf173d2904f 150
lrucker7 16:f9e3df933304 151 //pc.printf("this is running 100");
lrucker7 16:f9e3df933304 152 Thread::wait(10);
lrucker7 16:f9e3df933304 153 }
pandirimukund 12:1fc4b3d94397 154 }
pandirimukund 12:1fc4b3d94397 155
pandirimukund 12:1fc4b3d94397 156
pandirimukund 12:1fc4b3d94397 157 ///////////////////////////////////////////////////////////////////////////////
pandirimukund 12:1fc4b3d94397 158 ///////////////////////////// IMU STUFF////////////////////////////////////////
pandirimukund 12:1fc4b3d94397 159 ///////////////////////////////////////////////////////////////////////////////
pandirimukund 12:1fc4b3d94397 160 void calibrate_imu() {
pandirimukund 17:afde478daa01 161 for(int i = 0; i < 500; i++){
pandirimukund 17:afde478daa01 162 imu.readGyro();
pandirimukund 17:afde478daa01 163 angleBias += imu.gy;
pandirimukund 17:afde478daa01 164 }
pandirimukund 17:afde478daa01 165 angleBias /= 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 17:afde478daa01 171 int gyro = -(imu.gy-angleBias) * .01;
lrucker7 16:f9e3df933304 172 //pc.printf("gyro:%f",gyro);
pandirimukund 15:f0f19572c4a5 173 angleMutex.lock();
pandirimukund 27:d053966e9320 174 dAngle = pAngle;
pandirimukund 27:d053966e9320 175
pandirimukund 27:d053966e9320 176 pAngle += 1*gyro; //+ 0.02*acc;
pandirimukund 27:d053966e9320 177 pAngle -= desired_angle*70;
pandirimukund 27:d053966e9320 178
pandirimukund 27:d053966e9320 179 dAngle = pAngle - dAngle;
pandirimukund 27:d053966e9320 180
pandirimukund 27:d053966e9320 181 iAngle += pAngle * 0.01;
pandirimukund 27:d053966e9320 182
pandirimukund 15:f0f19572c4a5 183 angleMutex.unlock();
pandirimukund 12:1fc4b3d94397 184 }
pandirimukund 12:1fc4b3d94397 185
pandirimukund 12:1fc4b3d94397 186
pandirimukund 12:1fc4b3d94397 187
pandirimukund 12:1fc4b3d94397 188
pandirimukund 12:1fc4b3d94397 189 ///////////////////////////////////////////////////////////////////////////////
pandirimukund 12:1fc4b3d94397 190 ///////////////////////////// Running Main Function////////////////////////////
pandirimukund 12:1fc4b3d94397 191 ///////////////////////////////////////////////////////////////////////////////
pandirimukund 12:1fc4b3d94397 192
emilmont 1:491820ee784d 193 int main() {
pandirimukund 12:1fc4b3d94397 194 pc.printf("this is running");
lrucker7 23:d2dcd3f9c309 195
pandirimukund 12:1fc4b3d94397 196 Thread bluetooth;
pandirimukund 12:1fc4b3d94397 197 Thread system_update;
emilmont 1:491820ee784d 198
pandirimukund 14:f9c2cf6643cf 199 imu.begin();
lrucker7 23:d2dcd3f9c309 200 calibrate_imu();
pandirimukund 12:1fc4b3d94397 201 bluetooth.start(bluetooth_update);
pandirimukund 12:1fc4b3d94397 202 system_update.start(update_system);
pandirimukund 12:1fc4b3d94397 203
pandirimukund 12:1fc4b3d94397 204 //bluetooth.attach(&bluetooth_update, 0.1);
pandirimukund 12:1fc4b3d94397 205 while (1) {
lrucker7 16:f9e3df933304 206
pandirimukund 12:1fc4b3d94397 207 //bluetooth_update();
lrucker7 16:f9e3df933304 208 //parametersmutex.lock();
lrucker7 16:f9e3df933304 209 // pc.printf("rp: %f, rd: %f, ri: %f, desired_angle: %f\n", rp, rd, ri, desired_angle);
lrucker7 16:f9e3df933304 210 // parametersmutex.unlock();
pandirimukund 12:1fc4b3d94397 211
pandirimukund 15:f0f19572c4a5 212 angleMutex.lock();
pandirimukund 15:f0f19572c4a5 213 pc.printf("pAngle: %f", pAngle);
pandirimukund 26:dcf173d2904f 214 pc.printf("speed: %f", speed);
pandirimukund 15:f0f19572c4a5 215 angleMutex.unlock();
pandirimukund 15:f0f19572c4a5 216
pandirimukund 15:f0f19572c4a5 217
lrucker7 16:f9e3df933304 218 //get_current_angle();
pandirimukund 14:f9c2cf6643cf 219
pandirimukund 12:1fc4b3d94397 220
pandirimukund 12:1fc4b3d94397 221 Thread::wait(100);
emilmont 1:491820ee784d 222 }
emilmont 1:491820ee784d 223 }
pandirimukund 12:1fc4b3d94397 224