segway_self balancing robot 4180 project
Dependencies: mbed mbed-rtos LSM9DS1_Library
main.cpp@9:28821420f7a4, 2020-04-18 (annotated)
- Committer:
- lrucker7
- Date:
- Sat Apr 18 20:03:58 2020 +0000
- Revision:
- 9:28821420f7a4
- Parent:
- 8:2332c4a79276
- Child:
- 10:aafadd98d91c
- Child:
- 11:c669b4dc1f9f
Bluetooth initially working
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; |
lrucker7 | 9:28821420f7a4 | 19 | |
lrucker7 | 9:28821420f7a4 | 20 | while (blue.getc()!='!') { |
lrucker7 | 9:28821420f7a4 | 21 | |
lrucker7 | 9:28821420f7a4 | 22 | } |
pandirimukund | 7:8af26d50c006 | 23 | if (blue.getc()=='B') { //button data packet |
pandirimukund | 7:8af26d50c006 | 24 | bnum = blue.getc(); //button number |
lrucker7 | 9:28821420f7a4 | 25 | pc.printf("%d",bnum); |
pandirimukund | 7:8af26d50c006 | 26 | bhit = blue.getc(); //1=hit, 0=release |
pandirimukund | 7:8af26d50c006 | 27 | if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK? |
pandirimukund | 7:8af26d50c006 | 28 | myled = bnum - '0'; //current button number will appear on LEDs |
pandirimukund | 7:8af26d50c006 | 29 | switch (bnum) { |
pandirimukund | 7:8af26d50c006 | 30 | case '1': //number button 1 |
pandirimukund | 7:8af26d50c006 | 31 | if (bhit=='1') { |
pandirimukund | 7:8af26d50c006 | 32 | rd += 1; |
pandirimukund | 7:8af26d50c006 | 33 | } else { |
pandirimukund | 7:8af26d50c006 | 34 | //add release code here |
pandirimukund | 7:8af26d50c006 | 35 | } |
pandirimukund | 7:8af26d50c006 | 36 | break; |
pandirimukund | 7:8af26d50c006 | 37 | case '2': //number button 2 |
pandirimukund | 7:8af26d50c006 | 38 | if (bhit=='1') { |
pandirimukund | 7:8af26d50c006 | 39 | ri += 1; |
pandirimukund | 7:8af26d50c006 | 40 | } else { |
pandirimukund | 7:8af26d50c006 | 41 | //add release code here |
pandirimukund | 7:8af26d50c006 | 42 | } |
pandirimukund | 7:8af26d50c006 | 43 | break; |
pandirimukund | 7:8af26d50c006 | 44 | case '3': //number button 3 |
pandirimukund | 7:8af26d50c006 | 45 | if (bhit=='1') { |
pandirimukund | 7:8af26d50c006 | 46 | rd -= 1; |
pandirimukund | 7:8af26d50c006 | 47 | } else { |
pandirimukund | 7:8af26d50c006 | 48 | //add release code here |
pandirimukund | 7:8af26d50c006 | 49 | } |
pandirimukund | 7:8af26d50c006 | 50 | break; |
pandirimukund | 7:8af26d50c006 | 51 | case '4': //number button 4 |
pandirimukund | 7:8af26d50c006 | 52 | if (bhit=='1') { |
pandirimukund | 7:8af26d50c006 | 53 | ri -= 1; |
pandirimukund | 7:8af26d50c006 | 54 | } else { |
pandirimukund | 7:8af26d50c006 | 55 | //add release code here |
pandirimukund | 7:8af26d50c006 | 56 | } |
pandirimukund | 7:8af26d50c006 | 57 | break; |
pandirimukund | 7:8af26d50c006 | 58 | case '5': //button 5 up arrow |
pandirimukund | 7:8af26d50c006 | 59 | if (bhit=='1') { |
pandirimukund | 7:8af26d50c006 | 60 | rp += 1; |
pandirimukund | 7:8af26d50c006 | 61 | } else { |
pandirimukund | 7:8af26d50c006 | 62 | //add release code here |
pandirimukund | 7:8af26d50c006 | 63 | } |
pandirimukund | 7:8af26d50c006 | 64 | break; |
pandirimukund | 7:8af26d50c006 | 65 | case '6': //button 6 down arrow |
pandirimukund | 7:8af26d50c006 | 66 | if (bhit=='1') { |
pandirimukund | 7:8af26d50c006 | 67 | rp -= 1; |
pandirimukund | 7:8af26d50c006 | 68 | } else { |
pandirimukund | 7:8af26d50c006 | 69 | //add release code here |
pandirimukund | 7:8af26d50c006 | 70 | } |
pandirimukund | 7:8af26d50c006 | 71 | break; |
pandirimukund | 7:8af26d50c006 | 72 | case '7': //button 7 left arrow |
pandirimukund | 7:8af26d50c006 | 73 | if (bhit=='1') { |
pandirimukund | 7:8af26d50c006 | 74 | desired_angle -= 1; |
pandirimukund | 7:8af26d50c006 | 75 | } else { |
pandirimukund | 7:8af26d50c006 | 76 | //add release code here |
pandirimukund | 7:8af26d50c006 | 77 | } |
pandirimukund | 7:8af26d50c006 | 78 | break; |
pandirimukund | 7:8af26d50c006 | 79 | case '8': //button 8 right arrow |
pandirimukund | 7:8af26d50c006 | 80 | if (bhit=='1') { |
pandirimukund | 7:8af26d50c006 | 81 | desired_angle += 1; |
pandirimukund | 7:8af26d50c006 | 82 | } else { |
pandirimukund | 7:8af26d50c006 | 83 | //add release code here |
pandirimukund | 7:8af26d50c006 | 84 | } |
pandirimukund | 7:8af26d50c006 | 85 | break; |
pandirimukund | 7:8af26d50c006 | 86 | default: |
pandirimukund | 7:8af26d50c006 | 87 | break; |
pandirimukund | 7:8af26d50c006 | 88 | } |
pandirimukund | 7:8af26d50c006 | 89 | } |
pandirimukund | 7:8af26d50c006 | 90 | } |
lrucker7 | 9:28821420f7a4 | 91 | //} |
pandirimukund | 0:49139eec4c55 | 92 | } |
pandirimukund | 7:8af26d50c006 | 93 | |
pandirimukund | 7:8af26d50c006 | 94 | int main() { |
pandirimukund | 7:8af26d50c006 | 95 | pc.printf("this is running"); |
lrucker7 | 9:28821420f7a4 | 96 | //bluetooth.attach(&bluetooth_update, 0.1); |
pandirimukund | 7:8af26d50c006 | 97 | while(1) { |
lrucker7 | 9:28821420f7a4 | 98 | bluetooth_update(); |
lrucker7 | 9:28821420f7a4 | 99 | pc.printf("rp: %f, rd: %f, ri: %f, desired_angle: %f\n", rp, rd, ri, desired_angle); |
pandirimukund | 8:2332c4a79276 | 100 | wait(0.1); |
pandirimukund | 7:8af26d50c006 | 101 | } |
pandirimukund | 7:8af26d50c006 | 102 | } |
pandirimukund | 7:8af26d50c006 | 103 |