segway_self balancing robot 4180 project

Dependencies:   mbed mbed-rtos LSM9DS1_Library

Committer:
pandirimukund
Date:
Sat Apr 18 20:13:31 2020 +0000
Revision:
10:aafadd98d91c
Parent:
9:28821420f7a4
changed to rtos;

Who changed what in which revision?

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