segway_self balancing robot 4180 project
Dependencies: mbed mbed-rtos LSM9DS1_Library
main.cpp@8:2332c4a79276, 2020-04-18 (annotated)
- Committer:
- pandirimukund
- Date:
- Sat Apr 18 19:39:10 2020 +0000
- Revision:
- 8:2332c4a79276
- Parent:
- 7:8af26d50c006
- Child:
- 9:28821420f7a4
added the wait;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pandirimukund | 0:49139eec4c55 | 1 | #include "mbed.h" |
pandirimukund | 0:49139eec4c55 | 2 | |
fmmgramacho | 6:0bff4e260db2 | 3 | DigitalOut myled(LED2); |
pandirimukund | 0:49139eec4c55 | 4 | |
pandirimukund | 7:8af26d50c006 | 5 | Ticker bluetooth; |
pandirimukund | 7:8af26d50c006 | 6 | Serial pc(USBTX, USBRX); |
pandirimukund | 7:8af26d50c006 | 7 | |
pandirimukund | 7:8af26d50c006 | 8 | |
pandirimukund | 7:8af26d50c006 | 9 | //Control system variables |
pandirimukund | 7:8af26d50c006 | 10 | float rp = 50; |
pandirimukund | 7:8af26d50c006 | 11 | float rd = 51; |
pandirimukund | 7:8af26d50c006 | 12 | float ri = 50; |
pandirimukund | 7:8af26d50c006 | 13 | float desired_angle = 0; |
pandirimukund | 7:8af26d50c006 | 14 | Serial blue(p28,p27); |
pandirimukund | 7:8af26d50c006 | 15 | |
pandirimukund | 7:8af26d50c006 | 16 | void bluetooth_update() { |
pandirimukund | 7:8af26d50c006 | 17 | char bnum=0; |
pandirimukund | 7:8af26d50c006 | 18 | char bhit=0; |
pandirimukund | 7:8af26d50c006 | 19 | if (blue.getc()=='!') { |
pandirimukund | 7:8af26d50c006 | 20 | if (blue.getc()=='B') { //button data packet |
pandirimukund | 7:8af26d50c006 | 21 | bnum = blue.getc(); //button number |
pandirimukund | 7:8af26d50c006 | 22 | bhit = blue.getc(); //1=hit, 0=release |
pandirimukund | 7:8af26d50c006 | 23 | if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK? |
pandirimukund | 7:8af26d50c006 | 24 | myled = bnum - '0'; //current button number will appear on LEDs |
pandirimukund | 7:8af26d50c006 | 25 | switch (bnum) { |
pandirimukund | 7:8af26d50c006 | 26 | case '1': //number button 1 |
pandirimukund | 7:8af26d50c006 | 27 | if (bhit=='1') { |
pandirimukund | 7:8af26d50c006 | 28 | rd += 1; |
pandirimukund | 7:8af26d50c006 | 29 | } else { |
pandirimukund | 7:8af26d50c006 | 30 | //add release code here |
pandirimukund | 7:8af26d50c006 | 31 | } |
pandirimukund | 7:8af26d50c006 | 32 | break; |
pandirimukund | 7:8af26d50c006 | 33 | case '2': //number button 2 |
pandirimukund | 7:8af26d50c006 | 34 | if (bhit=='1') { |
pandirimukund | 7:8af26d50c006 | 35 | ri += 1; |
pandirimukund | 7:8af26d50c006 | 36 | } else { |
pandirimukund | 7:8af26d50c006 | 37 | //add release code here |
pandirimukund | 7:8af26d50c006 | 38 | } |
pandirimukund | 7:8af26d50c006 | 39 | break; |
pandirimukund | 7:8af26d50c006 | 40 | case '3': //number button 3 |
pandirimukund | 7:8af26d50c006 | 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 '4': //number button 4 |
pandirimukund | 7:8af26d50c006 | 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 '5': //button 5 up arrow |
pandirimukund | 7:8af26d50c006 | 55 | if (bhit=='1') { |
pandirimukund | 7:8af26d50c006 | 56 | rp += 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 '6': //button 6 down arrow |
pandirimukund | 7:8af26d50c006 | 62 | if (bhit=='1') { |
pandirimukund | 7:8af26d50c006 | 63 | rp -= 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 '7': //button 7 left arrow |
pandirimukund | 7:8af26d50c006 | 69 | if (bhit=='1') { |
pandirimukund | 7:8af26d50c006 | 70 | desired_angle -= 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 '8': //button 8 right arrow |
pandirimukund | 7:8af26d50c006 | 76 | if (bhit=='1') { |
pandirimukund | 7:8af26d50c006 | 77 | desired_angle += 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 | default: |
pandirimukund | 7:8af26d50c006 | 83 | break; |
pandirimukund | 7:8af26d50c006 | 84 | } |
pandirimukund | 7:8af26d50c006 | 85 | } |
pandirimukund | 7:8af26d50c006 | 86 | } |
pandirimukund | 0:49139eec4c55 | 87 | } |
pandirimukund | 0:49139eec4c55 | 88 | } |
pandirimukund | 7:8af26d50c006 | 89 | |
pandirimukund | 7:8af26d50c006 | 90 | int main() { |
pandirimukund | 7:8af26d50c006 | 91 | pc.printf("this is running"); |
pandirimukund | 7:8af26d50c006 | 92 | bluetooth.attach(&bluetooth_update, 0.1); |
pandirimukund | 7:8af26d50c006 | 93 | while(1) { |
pandirimukund | 7:8af26d50c006 | 94 | pc.printf("rp: %f, rd: %f, ri: %f, desired_angle: %f", rp, rd, ri, desired_angle); |
pandirimukund | 8:2332c4a79276 | 95 | wait(0.1); |
pandirimukund | 7:8af26d50c006 | 96 | } |
pandirimukund | 7:8af26d50c006 | 97 | } |
pandirimukund | 7:8af26d50c006 | 98 |