segway_self balancing robot 4180 project

Dependencies:   mbed mbed-rtos LSM9DS1_Library

Committer:
lrucker7
Date:
Sat Apr 18 20:35:58 2020 +0000
Revision:
11:c669b4dc1f9f
Parent:
9:28821420f7a4
Child:
12:980c06d63425
working mbed rtos with bluetooth

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pandirimukund 0:49139eec4c55 1 #include "mbed.h"
lrucker7 11:c669b4dc1f9f 2 #include "rtos.h"
pandirimukund 0:49139eec4c55 3
fmmgramacho 6:0bff4e260db2 4 DigitalOut myled(LED2);
pandirimukund 0:49139eec4c55 5
pandirimukund 7:8af26d50c006 6 Ticker bluetooth;
pandirimukund 7:8af26d50c006 7 Serial pc(USBTX, USBRX);
pandirimukund 7:8af26d50c006 8
lrucker7 11:c669b4dc1f9f 9 Mutex parametersmutex;
pandirimukund 7:8af26d50c006 10
pandirimukund 7:8af26d50c006 11 //Control system variables
pandirimukund 7:8af26d50c006 12 float rp = 50;
pandirimukund 7:8af26d50c006 13 float rd = 51;
pandirimukund 7:8af26d50c006 14 float ri = 50;
pandirimukund 7:8af26d50c006 15 float desired_angle = 0;
pandirimukund 7:8af26d50c006 16 Serial blue(p28,p27);
pandirimukund 7:8af26d50c006 17
pandirimukund 7:8af26d50c006 18 void bluetooth_update() {
pandirimukund 7:8af26d50c006 19 char bnum=0;
pandirimukund 7:8af26d50c006 20 char bhit=0;
lrucker7 11:c669b4dc1f9f 21 while(1){
lrucker7 11:c669b4dc1f9f 22 if (blue.getc()=='!') {
pandirimukund 7:8af26d50c006 23 if (blue.getc()=='B') { //button data packet
pandirimukund 7:8af26d50c006 24 bnum = blue.getc(); //button number
lrucker7 11:c669b4dc1f9f 25 //pc.printf("%d",bnum);
pandirimukund 7:8af26d50c006 26 bhit = blue.getc(); //1=hit, 0=release
lrucker7 11:c669b4dc1f9f 27 parametersmutex.lock();
pandirimukund 7:8af26d50c006 28 if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK?
pandirimukund 7:8af26d50c006 29 myled = bnum - '0'; //current button number will appear on LEDs
pandirimukund 7:8af26d50c006 30 switch (bnum) {
pandirimukund 7:8af26d50c006 31 case '1': //number button 1
pandirimukund 7:8af26d50c006 32 if (bhit=='1') {
pandirimukund 7:8af26d50c006 33 rd += 1;
pandirimukund 7:8af26d50c006 34 } else {
pandirimukund 7:8af26d50c006 35 //add release code here
pandirimukund 7:8af26d50c006 36 }
pandirimukund 7:8af26d50c006 37 break;
pandirimukund 7:8af26d50c006 38 case '2': //number button 2
pandirimukund 7:8af26d50c006 39 if (bhit=='1') {
pandirimukund 7:8af26d50c006 40 ri += 1;
pandirimukund 7:8af26d50c006 41 } else {
pandirimukund 7:8af26d50c006 42 //add release code here
pandirimukund 7:8af26d50c006 43 }
pandirimukund 7:8af26d50c006 44 break;
pandirimukund 7:8af26d50c006 45 case '3': //number button 3
pandirimukund 7:8af26d50c006 46 if (bhit=='1') {
pandirimukund 7:8af26d50c006 47 rd -= 1;
pandirimukund 7:8af26d50c006 48 } else {
pandirimukund 7:8af26d50c006 49 //add release code here
pandirimukund 7:8af26d50c006 50 }
pandirimukund 7:8af26d50c006 51 break;
pandirimukund 7:8af26d50c006 52 case '4': //number button 4
pandirimukund 7:8af26d50c006 53 if (bhit=='1') {
pandirimukund 7:8af26d50c006 54 ri -= 1;
pandirimukund 7:8af26d50c006 55 } else {
pandirimukund 7:8af26d50c006 56 //add release code here
pandirimukund 7:8af26d50c006 57 }
pandirimukund 7:8af26d50c006 58 break;
pandirimukund 7:8af26d50c006 59 case '5': //button 5 up arrow
pandirimukund 7:8af26d50c006 60 if (bhit=='1') {
pandirimukund 7:8af26d50c006 61 rp += 1;
pandirimukund 7:8af26d50c006 62 } else {
pandirimukund 7:8af26d50c006 63 //add release code here
pandirimukund 7:8af26d50c006 64 }
pandirimukund 7:8af26d50c006 65 break;
pandirimukund 7:8af26d50c006 66 case '6': //button 6 down arrow
pandirimukund 7:8af26d50c006 67 if (bhit=='1') {
pandirimukund 7:8af26d50c006 68 rp -= 1;
pandirimukund 7:8af26d50c006 69 } else {
pandirimukund 7:8af26d50c006 70 //add release code here
pandirimukund 7:8af26d50c006 71 }
pandirimukund 7:8af26d50c006 72 break;
pandirimukund 7:8af26d50c006 73 case '7': //button 7 left arrow
pandirimukund 7:8af26d50c006 74 if (bhit=='1') {
pandirimukund 7:8af26d50c006 75 desired_angle -= 1;
pandirimukund 7:8af26d50c006 76 } else {
pandirimukund 7:8af26d50c006 77 //add release code here
pandirimukund 7:8af26d50c006 78 }
pandirimukund 7:8af26d50c006 79 break;
pandirimukund 7:8af26d50c006 80 case '8': //button 8 right arrow
pandirimukund 7:8af26d50c006 81 if (bhit=='1') {
pandirimukund 7:8af26d50c006 82 desired_angle += 1;
pandirimukund 7:8af26d50c006 83 } else {
pandirimukund 7:8af26d50c006 84 //add release code here
pandirimukund 7:8af26d50c006 85 }
pandirimukund 7:8af26d50c006 86 break;
pandirimukund 7:8af26d50c006 87 default:
pandirimukund 7:8af26d50c006 88 break;
pandirimukund 7:8af26d50c006 89 }
pandirimukund 7:8af26d50c006 90 }
lrucker7 11:c669b4dc1f9f 91 parametersmutex.unlock();
pandirimukund 7:8af26d50c006 92 }
lrucker7 11:c669b4dc1f9f 93 }
lrucker7 11:c669b4dc1f9f 94 Thread::wait(100);
lrucker7 11:c669b4dc1f9f 95 }
pandirimukund 0:49139eec4c55 96 }
pandirimukund 7:8af26d50c006 97
pandirimukund 7:8af26d50c006 98 int main() {
pandirimukund 7:8af26d50c006 99 pc.printf("this is running");
lrucker7 11:c669b4dc1f9f 100 Thread bluetooth;
lrucker7 11:c669b4dc1f9f 101 bluetooth.start(bluetooth_update);
lrucker7 9:28821420f7a4 102 //bluetooth.attach(&bluetooth_update, 0.1);
pandirimukund 7:8af26d50c006 103 while(1) {
lrucker7 11:c669b4dc1f9f 104 //bluetooth_update();
lrucker7 11:c669b4dc1f9f 105 parametersmutex.lock();
lrucker7 9:28821420f7a4 106 pc.printf("rp: %f, rd: %f, ri: %f, desired_angle: %f\n", rp, rd, ri, desired_angle);
lrucker7 11:c669b4dc1f9f 107 parametersmutex.unlock();
lrucker7 11:c669b4dc1f9f 108 Thread::wait(100);
pandirimukund 7:8af26d50c006 109 }
pandirimukund 7:8af26d50c006 110 }
pandirimukund 7:8af26d50c006 111