self-balancing-robot
Dependencies: mbed mbed-rtos Motor
main.cpp@16:f9e3df933304, 2020-04-23 (annotated)
- Committer:
- lrucker7
- Date:
- Thu Apr 23 15:27:21 2020 +0000
- Revision:
- 16:f9e3df933304
- Parent:
- 15:f0f19572c4a5
- Child:
- 17:afde478daa01
preliminary gyro
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 | 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 | 12:1fc4b3d94397 | 38 | |
pandirimukund | 12:1fc4b3d94397 | 39 | /////////////////////////////////////////////////////////////////////////////// |
pandirimukund | 12:1fc4b3d94397 | 40 | ///////////////////////////// Bluetooth Section /////////////////////////////// |
pandirimukund | 12:1fc4b3d94397 | 41 | /////////////////////////////////////////////////////////////////////////////// |
pandirimukund | 12:1fc4b3d94397 | 42 | void bluetooth_update() { |
pandirimukund | 12:1fc4b3d94397 | 43 | char bnum = 0; |
pandirimukund | 12:1fc4b3d94397 | 44 | char bhit = 0; |
pandirimukund | 12:1fc4b3d94397 | 45 | while (1) { |
pandirimukund | 12:1fc4b3d94397 | 46 | if (blue.getc() == '!') { |
pandirimukund | 12:1fc4b3d94397 | 47 | if (blue.getc() == 'B') { //button data packet |
pandirimukund | 12:1fc4b3d94397 | 48 | bnum = blue.getc(); //button number |
pandirimukund | 12:1fc4b3d94397 | 49 | //pc.printf("%d",bnum); |
pandirimukund | 12:1fc4b3d94397 | 50 | bhit = blue.getc(); //1=hit, 0=release |
pandirimukund | 12:1fc4b3d94397 | 51 | parametersmutex.lock(); |
pandirimukund | 12:1fc4b3d94397 | 52 | if (blue.getc() == char(~('!' + 'B' + bnum + bhit))) { //checksum OK? |
pandirimukund | 12:1fc4b3d94397 | 53 | switch (bnum) { |
pandirimukund | 12:1fc4b3d94397 | 54 | case '1': //number button 1 |
pandirimukund | 12:1fc4b3d94397 | 55 | if (bhit == '1') { |
pandirimukund | 12:1fc4b3d94397 | 56 | rd += 1; |
pandirimukund | 12:1fc4b3d94397 | 57 | } else { |
pandirimukund | 12:1fc4b3d94397 | 58 | //add release code here |
pandirimukund | 12:1fc4b3d94397 | 59 | } |
pandirimukund | 12:1fc4b3d94397 | 60 | break; |
pandirimukund | 12:1fc4b3d94397 | 61 | case '2': //number button 2 |
pandirimukund | 12:1fc4b3d94397 | 62 | if (bhit == '1') { |
pandirimukund | 12:1fc4b3d94397 | 63 | ri += 1; |
pandirimukund | 12:1fc4b3d94397 | 64 | } else { |
pandirimukund | 12:1fc4b3d94397 | 65 | //add release code here |
pandirimukund | 12:1fc4b3d94397 | 66 | } |
pandirimukund | 12:1fc4b3d94397 | 67 | break; |
pandirimukund | 12:1fc4b3d94397 | 68 | case '3': //number button 3 |
pandirimukund | 12:1fc4b3d94397 | 69 | if (bhit == '1') { |
pandirimukund | 12:1fc4b3d94397 | 70 | rd -= 1; |
pandirimukund | 12:1fc4b3d94397 | 71 | } else { |
pandirimukund | 12:1fc4b3d94397 | 72 | //add release code here |
pandirimukund | 12:1fc4b3d94397 | 73 | } |
pandirimukund | 12:1fc4b3d94397 | 74 | break; |
pandirimukund | 12:1fc4b3d94397 | 75 | case '4': //number button 4 |
pandirimukund | 12:1fc4b3d94397 | 76 | if (bhit == '1') { |
pandirimukund | 12:1fc4b3d94397 | 77 | ri -= 1; |
pandirimukund | 12:1fc4b3d94397 | 78 | } else { |
pandirimukund | 12:1fc4b3d94397 | 79 | //add release code here |
pandirimukund | 12:1fc4b3d94397 | 80 | } |
pandirimukund | 12:1fc4b3d94397 | 81 | break; |
pandirimukund | 12:1fc4b3d94397 | 82 | case '5': //button 5 up arrow |
pandirimukund | 12:1fc4b3d94397 | 83 | if (bhit == '1') { |
pandirimukund | 12:1fc4b3d94397 | 84 | rp += 1; |
pandirimukund | 12:1fc4b3d94397 | 85 | } else { |
pandirimukund | 12:1fc4b3d94397 | 86 | //add release code here |
pandirimukund | 12:1fc4b3d94397 | 87 | } |
pandirimukund | 12:1fc4b3d94397 | 88 | break; |
pandirimukund | 12:1fc4b3d94397 | 89 | case '6': //button 6 down arrow |
pandirimukund | 12:1fc4b3d94397 | 90 | if (bhit == '1') { |
pandirimukund | 12:1fc4b3d94397 | 91 | rp -= 1; |
pandirimukund | 12:1fc4b3d94397 | 92 | } else { |
pandirimukund | 12:1fc4b3d94397 | 93 | //add release code here |
pandirimukund | 12:1fc4b3d94397 | 94 | } |
pandirimukund | 12:1fc4b3d94397 | 95 | break; |
pandirimukund | 12:1fc4b3d94397 | 96 | case '7': //button 7 left arrow |
pandirimukund | 12:1fc4b3d94397 | 97 | if (bhit == '1') { |
pandirimukund | 12:1fc4b3d94397 | 98 | desired_angle -= 1; |
pandirimukund | 12:1fc4b3d94397 | 99 | } else { |
pandirimukund | 12:1fc4b3d94397 | 100 | //add release code here |
pandirimukund | 12:1fc4b3d94397 | 101 | } |
pandirimukund | 12:1fc4b3d94397 | 102 | break; |
pandirimukund | 12:1fc4b3d94397 | 103 | case '8': //button 8 right arrow |
pandirimukund | 12:1fc4b3d94397 | 104 | if (bhit == '1') { |
pandirimukund | 12:1fc4b3d94397 | 105 | desired_angle += 1; |
pandirimukund | 12:1fc4b3d94397 | 106 | } else { |
pandirimukund | 12:1fc4b3d94397 | 107 | //add release code here |
pandirimukund | 12:1fc4b3d94397 | 108 | } |
pandirimukund | 12:1fc4b3d94397 | 109 | break; |
pandirimukund | 12:1fc4b3d94397 | 110 | default: |
pandirimukund | 12:1fc4b3d94397 | 111 | break; |
pandirimukund | 12:1fc4b3d94397 | 112 | } |
pandirimukund | 12:1fc4b3d94397 | 113 | } |
pandirimukund | 12:1fc4b3d94397 | 114 | parametersmutex.unlock(); |
pandirimukund | 12:1fc4b3d94397 | 115 | } |
pandirimukund | 12:1fc4b3d94397 | 116 | } |
pandirimukund | 12:1fc4b3d94397 | 117 | Thread::wait(100); |
emilmont | 1:491820ee784d | 118 | } |
emilmont | 1:491820ee784d | 119 | } |
pandirimukund | 12:1fc4b3d94397 | 120 | |
pandirimukund | 12:1fc4b3d94397 | 121 | |
pandirimukund | 12:1fc4b3d94397 | 122 | /////////////////////////////////////////////////////////////////////////////// |
pandirimukund | 12:1fc4b3d94397 | 123 | ///////////////////////////// Control System Updates/////////////////////////// |
pandirimukund | 12:1fc4b3d94397 | 124 | /////////////////////////////////////////////////////////////////////////////// |
pandirimukund | 12:1fc4b3d94397 | 125 | //make the calls to IMU here and this should be another thread |
pandirimukund | 12:1fc4b3d94397 | 126 | void update_system() { |
lrucker7 | 16:f9e3df933304 | 127 | while(1){ |
lrucker7 | 16:f9e3df933304 | 128 | get_current_angle(); |
lrucker7 | 16:f9e3df933304 | 129 | //pc.printf("this is running 100"); |
lrucker7 | 16:f9e3df933304 | 130 | Thread::wait(10); |
lrucker7 | 16:f9e3df933304 | 131 | } |
pandirimukund | 12:1fc4b3d94397 | 132 | } |
pandirimukund | 12:1fc4b3d94397 | 133 | |
pandirimukund | 12:1fc4b3d94397 | 134 | |
pandirimukund | 12:1fc4b3d94397 | 135 | /////////////////////////////////////////////////////////////////////////////// |
pandirimukund | 12:1fc4b3d94397 | 136 | ///////////////////////////// IMU STUFF//////////////////////////////////////// |
pandirimukund | 12:1fc4b3d94397 | 137 | /////////////////////////////////////////////////////////////////////////////// |
pandirimukund | 12:1fc4b3d94397 | 138 | void calibrate_imu() { |
pandirimukund | 12:1fc4b3d94397 | 139 | return; |
pandirimukund | 12:1fc4b3d94397 | 140 | } |
pandirimukund | 12:1fc4b3d94397 | 141 | |
pandirimukund | 12:1fc4b3d94397 | 142 | void get_current_angle() { |
pandirimukund | 13:8d8ac3189984 | 143 | // return; |
pandirimukund | 13:8d8ac3189984 | 144 | imu.readGyro(); |
pandirimukund | 15:f0f19572c4a5 | 145 | int gyro = -imu.gy * .01; |
lrucker7 | 16:f9e3df933304 | 146 | //pc.printf("gyro:%f",gyro); |
pandirimukund | 15:f0f19572c4a5 | 147 | angleMutex.lock(); |
pandirimukund | 15:f0f19572c4a5 | 148 | pAngle += gyro; |
pandirimukund | 15:f0f19572c4a5 | 149 | angleMutex.unlock(); |
pandirimukund | 12:1fc4b3d94397 | 150 | } |
pandirimukund | 12:1fc4b3d94397 | 151 | |
pandirimukund | 12:1fc4b3d94397 | 152 | |
pandirimukund | 12:1fc4b3d94397 | 153 | |
pandirimukund | 12:1fc4b3d94397 | 154 | |
pandirimukund | 12:1fc4b3d94397 | 155 | /////////////////////////////////////////////////////////////////////////////// |
pandirimukund | 12:1fc4b3d94397 | 156 | ///////////////////////////// Running Main Function//////////////////////////// |
pandirimukund | 12:1fc4b3d94397 | 157 | /////////////////////////////////////////////////////////////////////////////// |
pandirimukund | 12:1fc4b3d94397 | 158 | |
emilmont | 1:491820ee784d | 159 | int main() { |
pandirimukund | 12:1fc4b3d94397 | 160 | pc.printf("this is running"); |
pandirimukund | 12:1fc4b3d94397 | 161 | Thread bluetooth; |
pandirimukund | 12:1fc4b3d94397 | 162 | Thread system_update; |
emilmont | 1:491820ee784d | 163 | |
pandirimukund | 14:f9c2cf6643cf | 164 | imu.begin(); |
pandirimukund | 12:1fc4b3d94397 | 165 | |
pandirimukund | 12:1fc4b3d94397 | 166 | bluetooth.start(bluetooth_update); |
pandirimukund | 12:1fc4b3d94397 | 167 | system_update.start(update_system); |
pandirimukund | 12:1fc4b3d94397 | 168 | |
pandirimukund | 12:1fc4b3d94397 | 169 | //bluetooth.attach(&bluetooth_update, 0.1); |
pandirimukund | 12:1fc4b3d94397 | 170 | while (1) { |
lrucker7 | 16:f9e3df933304 | 171 | |
pandirimukund | 12:1fc4b3d94397 | 172 | //bluetooth_update(); |
lrucker7 | 16:f9e3df933304 | 173 | //parametersmutex.lock(); |
lrucker7 | 16:f9e3df933304 | 174 | // pc.printf("rp: %f, rd: %f, ri: %f, desired_angle: %f\n", rp, rd, ri, desired_angle); |
lrucker7 | 16:f9e3df933304 | 175 | // parametersmutex.unlock(); |
pandirimukund | 12:1fc4b3d94397 | 176 | |
pandirimukund | 15:f0f19572c4a5 | 177 | angleMutex.lock(); |
pandirimukund | 15:f0f19572c4a5 | 178 | pc.printf("pAngle: %f", pAngle); |
pandirimukund | 15:f0f19572c4a5 | 179 | angleMutex.unlock(); |
pandirimukund | 15:f0f19572c4a5 | 180 | |
pandirimukund | 15:f0f19572c4a5 | 181 | |
lrucker7 | 16:f9e3df933304 | 182 | //get_current_angle(); |
pandirimukund | 14:f9c2cf6643cf | 183 | |
pandirimukund | 12:1fc4b3d94397 | 184 | |
pandirimukund | 12:1fc4b3d94397 | 185 | Thread::wait(100); |
emilmont | 1:491820ee784d | 186 | } |
emilmont | 1:491820ee784d | 187 | } |
pandirimukund | 12:1fc4b3d94397 | 188 |