segway_self balancing robot 4180 project
Dependencies: mbed mbed-rtos LSM9DS1_Library
main.cpp@13:c00ddcfea79f, 2020-04-18 (annotated)
- Committer:
- pandirimukund
- Date:
- Sat Apr 18 20:49:31 2020 +0000
- Revision:
- 13:c00ddcfea79f
- Parent:
- 12:980c06d63425
- Child:
- 14:89bb16d03b15
made baseline layout for the code;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pandirimukund | 0:49139eec4c55 | 1 | #include "mbed.h" |
lrucker7 | 11:c669b4dc1f9f | 2 | #include "rtos.h" |
pandirimukund | 0:49139eec4c55 | 3 | |
pandirimukund | 0:49139eec4c55 | 4 | |
pandirimukund | 13:c00ddcfea79f | 5 | /////////////////////////////////////////////////////////////////////////////// |
pandirimukund | 13:c00ddcfea79f | 6 | ///////////////////////////// Variable Initialization////////////////////////// |
pandirimukund | 13:c00ddcfea79f | 7 | /////////////////////////////////////////////////////////////////////////////// |
pandirimukund | 7:8af26d50c006 | 8 | Ticker bluetooth; |
pandirimukund | 7:8af26d50c006 | 9 | Serial pc(USBTX, USBRX); |
pandirimukund | 7:8af26d50c006 | 10 | |
lrucker7 | 11:c669b4dc1f9f | 11 | Mutex parametersmutex; |
pandirimukund | 13:c00ddcfea79f | 12 | Serial blue(p28, p27); |
pandirimukund | 7:8af26d50c006 | 13 | |
pandirimukund | 13:c00ddcfea79f | 14 | /////////////////////////////////////////////////////////////////////////////// |
pandirimukund | 13:c00ddcfea79f | 15 | ///////////////////////////// Control System Variables///////////////////////// |
pandirimukund | 13:c00ddcfea79f | 16 | /////////////////////////////////////////////////////////////////////////////// |
pandirimukund | 7:8af26d50c006 | 17 | float rp = 50; |
pandirimukund | 7:8af26d50c006 | 18 | float rd = 51; |
pandirimukund | 7:8af26d50c006 | 19 | float ri = 50; |
pandirimukund | 7:8af26d50c006 | 20 | float desired_angle = 0; |
pandirimukund | 13:c00ddcfea79f | 21 | |
pandirimukund | 13:c00ddcfea79f | 22 | |
pandirimukund | 7:8af26d50c006 | 23 | |
pandirimukund | 13:c00ddcfea79f | 24 | /////////////////////////////////////////////////////////////////////////////// |
pandirimukund | 13:c00ddcfea79f | 25 | ///////////////////////////// Bluetooth Section /////////////////////////////// |
pandirimukund | 13:c00ddcfea79f | 26 | /////////////////////////////////////////////////////////////////////////////// |
pandirimukund | 7:8af26d50c006 | 27 | void bluetooth_update() { |
pandirimukund | 12:980c06d63425 | 28 | char bnum = 0; |
pandirimukund | 12:980c06d63425 | 29 | char bhit = 0; |
pandirimukund | 12:980c06d63425 | 30 | while (1) { |
pandirimukund | 12:980c06d63425 | 31 | if (blue.getc() == '!') { |
pandirimukund | 12:980c06d63425 | 32 | if (blue.getc() == 'B') { //button data packet |
pandirimukund | 12:980c06d63425 | 33 | bnum = blue.getc(); //button number |
pandirimukund | 12:980c06d63425 | 34 | //pc.printf("%d",bnum); |
pandirimukund | 12:980c06d63425 | 35 | bhit = blue.getc(); //1=hit, 0=release |
pandirimukund | 12:980c06d63425 | 36 | parametersmutex.lock(); |
pandirimukund | 12:980c06d63425 | 37 | if (blue.getc() == char(~('!' + 'B' + bnum + bhit))) { //checksum OK? |
pandirimukund | 12:980c06d63425 | 38 | myled = bnum - '0'; //current button number will appear on LEDs |
pandirimukund | 12:980c06d63425 | 39 | switch (bnum) { |
pandirimukund | 7:8af26d50c006 | 40 | case '1': //number button 1 |
pandirimukund | 12:980c06d63425 | 41 | if (bhit == '1') { |
pandirimukund | 7:8af26d50c006 | 42 | rd += 1; |
pandirimukund | 7:8af26d50c006 | 43 | } else { |
pandirimukund | 7:8af26d50c006 | 44 | //add release code here |
pandirimukund | 7:8af26d50c006 | 45 | } |
pandirimukund | 7:8af26d50c006 | 46 | break; |
pandirimukund | 7:8af26d50c006 | 47 | case '2': //number button 2 |
pandirimukund | 12:980c06d63425 | 48 | if (bhit == '1') { |
pandirimukund | 7:8af26d50c006 | 49 | ri += 1; |
pandirimukund | 7:8af26d50c006 | 50 | } else { |
pandirimukund | 7:8af26d50c006 | 51 | //add release code here |
pandirimukund | 7:8af26d50c006 | 52 | } |
pandirimukund | 7:8af26d50c006 | 53 | break; |
pandirimukund | 7:8af26d50c006 | 54 | case '3': //number button 3 |
pandirimukund | 12:980c06d63425 | 55 | if (bhit == '1') { |
pandirimukund | 7:8af26d50c006 | 56 | rd -= 1; |
pandirimukund | 7:8af26d50c006 | 57 | } else { |
pandirimukund | 7:8af26d50c006 | 58 | //add release code here |
pandirimukund | 7:8af26d50c006 | 59 | } |
pandirimukund | 7:8af26d50c006 | 60 | break; |
pandirimukund | 7:8af26d50c006 | 61 | case '4': //number button 4 |
pandirimukund | 12:980c06d63425 | 62 | if (bhit == '1') { |
pandirimukund | 7:8af26d50c006 | 63 | ri -= 1; |
pandirimukund | 7:8af26d50c006 | 64 | } else { |
pandirimukund | 7:8af26d50c006 | 65 | //add release code here |
pandirimukund | 7:8af26d50c006 | 66 | } |
pandirimukund | 7:8af26d50c006 | 67 | break; |
pandirimukund | 7:8af26d50c006 | 68 | case '5': //button 5 up arrow |
pandirimukund | 12:980c06d63425 | 69 | if (bhit == '1') { |
pandirimukund | 7:8af26d50c006 | 70 | rp += 1; |
pandirimukund | 7:8af26d50c006 | 71 | } else { |
pandirimukund | 7:8af26d50c006 | 72 | //add release code here |
pandirimukund | 7:8af26d50c006 | 73 | } |
pandirimukund | 7:8af26d50c006 | 74 | break; |
pandirimukund | 7:8af26d50c006 | 75 | case '6': //button 6 down arrow |
pandirimukund | 12:980c06d63425 | 76 | if (bhit == '1') { |
pandirimukund | 7:8af26d50c006 | 77 | rp -= 1; |
pandirimukund | 7:8af26d50c006 | 78 | } else { |
pandirimukund | 7:8af26d50c006 | 79 | //add release code here |
pandirimukund | 7:8af26d50c006 | 80 | } |
pandirimukund | 7:8af26d50c006 | 81 | break; |
pandirimukund | 7:8af26d50c006 | 82 | case '7': //button 7 left arrow |
pandirimukund | 12:980c06d63425 | 83 | if (bhit == '1') { |
pandirimukund | 7:8af26d50c006 | 84 | desired_angle -= 1; |
pandirimukund | 7:8af26d50c006 | 85 | } else { |
pandirimukund | 7:8af26d50c006 | 86 | //add release code here |
pandirimukund | 7:8af26d50c006 | 87 | } |
pandirimukund | 7:8af26d50c006 | 88 | break; |
pandirimukund | 7:8af26d50c006 | 89 | case '8': //button 8 right arrow |
pandirimukund | 12:980c06d63425 | 90 | if (bhit == '1') { |
pandirimukund | 7:8af26d50c006 | 91 | desired_angle += 1; |
pandirimukund | 7:8af26d50c006 | 92 | } else { |
pandirimukund | 7:8af26d50c006 | 93 | //add release code here |
pandirimukund | 7:8af26d50c006 | 94 | } |
pandirimukund | 7:8af26d50c006 | 95 | break; |
pandirimukund | 7:8af26d50c006 | 96 | default: |
pandirimukund | 7:8af26d50c006 | 97 | break; |
pandirimukund | 12:980c06d63425 | 98 | } |
pandirimukund | 7:8af26d50c006 | 99 | } |
pandirimukund | 12:980c06d63425 | 100 | parametersmutex.unlock(); |
pandirimukund | 7:8af26d50c006 | 101 | } |
pandirimukund | 7:8af26d50c006 | 102 | } |
pandirimukund | 12:980c06d63425 | 103 | Thread::wait(100); |
lrucker7 | 11:c669b4dc1f9f | 104 | } |
pandirimukund | 0:49139eec4c55 | 105 | } |
pandirimukund | 7:8af26d50c006 | 106 | |
pandirimukund | 13:c00ddcfea79f | 107 | |
pandirimukund | 13:c00ddcfea79f | 108 | /////////////////////////////////////////////////////////////////////////////// |
pandirimukund | 13:c00ddcfea79f | 109 | ///////////////////////////// Control System Updates/////////////////////////// |
pandirimukund | 13:c00ddcfea79f | 110 | /////////////////////////////////////////////////////////////////////////////// |
pandirimukund | 13:c00ddcfea79f | 111 | //make the calls to IMU here and this should be another thread |
pandirimukund | 13:c00ddcfea79f | 112 | void update_system() { |
pandirimukund | 13:c00ddcfea79f | 113 | return; |
pandirimukund | 13:c00ddcfea79f | 114 | } |
pandirimukund | 13:c00ddcfea79f | 115 | |
pandirimukund | 13:c00ddcfea79f | 116 | |
pandirimukund | 13:c00ddcfea79f | 117 | /////////////////////////////////////////////////////////////////////////////// |
pandirimukund | 13:c00ddcfea79f | 118 | ///////////////////////////// IMU STUFF//////////////////////////////////////// |
pandirimukund | 13:c00ddcfea79f | 119 | /////////////////////////////////////////////////////////////////////////////// |
pandirimukund | 13:c00ddcfea79f | 120 | void calibrate_imu() { |
pandirimukund | 13:c00ddcfea79f | 121 | return; |
pandirimukund | 13:c00ddcfea79f | 122 | } |
pandirimukund | 13:c00ddcfea79f | 123 | |
pandirimukund | 13:c00ddcfea79f | 124 | void get_current_angle() { |
pandirimukund | 13:c00ddcfea79f | 125 | return; |
pandirimukund | 13:c00ddcfea79f | 126 | } |
pandirimukund | 13:c00ddcfea79f | 127 | |
pandirimukund | 13:c00ddcfea79f | 128 | |
pandirimukund | 13:c00ddcfea79f | 129 | |
pandirimukund | 13:c00ddcfea79f | 130 | |
pandirimukund | 13:c00ddcfea79f | 131 | /////////////////////////////////////////////////////////////////////////////// |
pandirimukund | 13:c00ddcfea79f | 132 | ///////////////////////////// Running Main Function//////////////////////////// |
pandirimukund | 13:c00ddcfea79f | 133 | /////////////////////////////////////////////////////////////////////////////// |
pandirimukund | 13:c00ddcfea79f | 134 | |
pandirimukund | 7:8af26d50c006 | 135 | int main() { |
pandirimukund | 7:8af26d50c006 | 136 | pc.printf("this is running"); |
lrucker7 | 11:c669b4dc1f9f | 137 | Thread bluetooth; |
pandirimukund | 13:c00ddcfea79f | 138 | Thread system_update; |
lrucker7 | 11:c669b4dc1f9f | 139 | bluetooth.start(bluetooth_update); |
pandirimukund | 13:c00ddcfea79f | 140 | system_update.start(update_system); |
lrucker7 | 9:28821420f7a4 | 141 | //bluetooth.attach(&bluetooth_update, 0.1); |
pandirimukund | 12:980c06d63425 | 142 | while (1) { |
lrucker7 | 11:c669b4dc1f9f | 143 | //bluetooth_update(); |
lrucker7 | 11:c669b4dc1f9f | 144 | parametersmutex.lock(); |
lrucker7 | 9:28821420f7a4 | 145 | pc.printf("rp: %f, rd: %f, ri: %f, desired_angle: %f\n", rp, rd, ri, desired_angle); |
lrucker7 | 11:c669b4dc1f9f | 146 | parametersmutex.unlock(); |
lrucker7 | 11:c669b4dc1f9f | 147 | Thread::wait(100); |
pandirimukund | 7:8af26d50c006 | 148 | } |
pandirimukund | 7:8af26d50c006 | 149 | } |
pandirimukund | 7:8af26d50c006 | 150 |