Integration of wheelchair control code, emergency button code and watchdog code

Dependencies:   wheelchaircontrol3 Watchdog ros_lib_kinetic

Committer:
t1jain
Date:
Wed Jun 26 18:24:00 2019 +0000
Revision:
11:e5823b1f5268
Parent:
10:b5f6337c3a20
Child:
12:14cac8544c97
Integration of wheelchair control code, emergency button code and watchdog code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
t1jain 11:e5823b1f5268 1
t1jain 11:e5823b1f5268 2 #include "mbed.h"
t1jain 11:e5823b1f5268 3 #include <ros.h>
t1jain 11:e5823b1f5268 4 #include <nav_msgs/Odometry.h> //contains both the twist and pose
t1jain 11:e5823b1f5268 5 #include "Watchdog.h"
t1jain 11:e5823b1f5268 6 #include "wheelchair.h"
t1jain 11:e5823b1f5268 7 #include "rtos.h"
t1jain 11:e5823b1f5268 8
t1jain 11:e5823b1f5268 9 double watchdogLimit = 1.00; // Set timeout limit for watchdog timer
t1jain 11:e5823b1f5268 10 int buttonCheck = 0;
t1jain 11:e5823b1f5268 11 //DigitalIn button(D4, PullDown);
t1jain 11:e5823b1f5268 12 //DigitalOut signal(D5);
ryanlin97 0:7e6b349182bc 13
t1jain 11:e5823b1f5268 14 QEI wheel (D10, D9, NC, 1200); // Initializes right encoder
t1jain 11:e5823b1f5268 15 DigitalIn pt3(D10, PullUp); // Pull up resistors to read analog signals into digital signals
t1jain 11:e5823b1f5268 16 DigitalIn pt4(D9, PullUp);
ryanlin97 0:7e6b349182bc 17
ryanlin97 5:90bf5f0d86e9 18
t1jain 11:e5823b1f5268 19 /*added*/
t1jain 11:e5823b1f5268 20 //DigitalIn e_button(D4); // Emergency button will start at HIGH
t1jain 11:e5823b1f5268 21
t1jain 11:e5823b1f5268 22 QEI wheelS (D7, D8, NC, 1200); // Initializes Left encoder
t1jain 11:e5823b1f5268 23 DigitalIn pt1(D7, PullUp); // Pull up resistors to read analog signals into digital signals
t1jain 11:e5823b1f5268 24 DigitalIn pt2(D8, PullUp);
t1jain 11:e5823b1f5268 25
t1jain 11:e5823b1f5268 26 int max_velocity;
t1jain 11:e5823b1f5268 27 //Timer testAccT;
t1jain 11:e5823b1f5268 28
t1jain 11:e5823b1f5268 29 AnalogIn x(A0); // Initializes analog axis for the joystick
t1jain 11:e5823b1f5268 30 AnalogIn y(A1);
t1jain 11:e5823b1f5268 31
t1jain 11:e5823b1f5268 32 DigitalOut up(D12); // Turn up speed mode for joystick
t1jain 11:e5823b1f5268 33 DigitalOut down(D13); // Turn down speed mode for joystick
t1jain 11:e5823b1f5268 34 DigitalOut on(D14); // Turn Wheelchair On
t1jain 11:e5823b1f5268 35 DigitalOut off(D15); // Turn Wheelchair Off
t1jain 11:e5823b1f5268 36 bool manual = false; // Turns chair joystic to automatic and viceverza
jvfausto 9:ca11e4db63a7 37
t1jain 11:e5823b1f5268 38 Serial pc(USBTX, USBRX, 57600); // Serial Monitor
t1jain 11:e5823b1f5268 39
t1jain 11:e5823b1f5268 40 VL53L1X sensor1(PB_11, PB_10, D0); // initializes ToF sensors
t1jain 11:e5823b1f5268 41 VL53L1X sensor2(PB_11, PB_10, D1);
t1jain 11:e5823b1f5268 42 VL53L1X sensor3(PB_11, PB_10, D2);
t1jain 11:e5823b1f5268 43 VL53L1X sensor4(PB_11, PB_10, D3);
t1jain 11:e5823b1f5268 44 VL53L1X sensor5(PB_11, PB_10, D4);
t1jain 11:e5823b1f5268 45 VL53L1X sensor6(PB_11, PB_10, D5);
t1jain 11:e5823b1f5268 46 VL53L1X sensor7(PB_11, PB_10, PE_14);
t1jain 11:e5823b1f5268 47 VL53L1X sensor8(PB_11, PB_10, PE_12);
t1jain 11:e5823b1f5268 48 VL53L1X sensor9(PB_11, PB_10, PE_10);
t1jain 11:e5823b1f5268 49 VL53L1X sensor10(PB_11, PB_10, PE_15);
t1jain 11:e5823b1f5268 50 VL53L1X sensor11(PB_11, PB_10, D6);
t1jain 11:e5823b1f5268 51 VL53L1X sensor12(PB_11, PB_10, D11);
t1jain 11:e5823b1f5268 52
t1jain 11:e5823b1f5268 53 VL53L1X* ToF[12] = {&sensor1, &sensor2, &sensor3, &sensor4, &sensor5, &sensor6,
t1jain 11:e5823b1f5268 54 &sensor7, &sensor8, &sensor9, &sensor10, &sensor11, &sensor12}; //puts ToF sensor pointers into an array
t1jain 11:e5823b1f5268 55 VL53L1X** ToFT = ToF;
jvfausto 9:ca11e4db63a7 56
t1jain 11:e5823b1f5268 57 Timer t; // Initialize time object t
t1jain 11:e5823b1f5268 58 EventQueue queue; // Class to organize threads
t1jain 11:e5823b1f5268 59 Wheelchair smart(xDir,yDir, &pc, &t, &wheel, &wheelS, ToFT); //Initialize wheelchair object
t1jain 11:e5823b1f5268 60 Thread compass; // Thread for compass
t1jain 11:e5823b1f5268 61 Thread velocity; // Thread for velosity
t1jain 11:e5823b1f5268 62 Thread assistSafe; // Thread for safety stuff
t1jain 11:e5823b1f5268 63 Thread emergencyButton; // Thread to check button state and reset device
t1jain 11:e5823b1f5268 64
t1jain 11:e5823b1f5268 65 // void emergencyButton () {
t1jain 11:e5823b1f5268 66 // while(1) {
t1jain 11:e5823b1f5268 67 // signal = 0;
t1jain 11:e5823b1f5268 68 // while(!button) {
t1jain 11:e5823b1f5268 69 //
t1jain 11:e5823b1f5268 70 // //Send something to stop wheelchair
t1jain 11:e5823b1f5268 71 // signal = 1;
t1jain 11:e5823b1f5268 72 //
t1jain 11:e5823b1f5268 73 // pc.printf("Hello there!\n");
t1jain 11:e5823b1f5268 74 // pc.printf("I'm dead\n\n\n");
t1jain 11:e5823b1f5268 75 //
t1jain 11:e5823b1f5268 76 // //Reset Board
t1jain 11:e5823b1f5268 77 // NVIC_SystemReset();
t1jain 11:e5823b1f5268 78 //
t1jain 11:e5823b1f5268 79 // }
t1jain 11:e5823b1f5268 80 //
t1jain 11:e5823b1f5268 81 // }
t1jain 11:e5823b1f5268 82 // }
t1jain 11:e5823b1f5268 83
t1jain 11:e5823b1f5268 84 int main(void) {
t1jain 11:e5823b1f5268 85 /* nh.initNode();
t1jain 11:e5823b1f5268 86 nh.advertise(chatter);
t1jain 11:e5823b1f5268 87 nh.advertise(chatter2);
t1jain 11:e5823b1f5268 88 nh.subscribe(sub);*/
t1jain 11:e5823b1f5268 89 //testAccT.start();
t1jain 11:e5823b1f5268 90
t1jain 11:e5823b1f5268 91
t1jain 11:e5823b1f5268 92
t1jain 11:e5823b1f5268 93 pc.printf("Before starting\r\n");
t1jain 11:e5823b1f5268 94 queue.call_every(SAMPLEFREQ, &smart, &Wheelchair::compass_thread); //Sets up sampling frequency of the compass_thread
t1jain 11:e5823b1f5268 95 queue.call_every(SAMPLEFREQ, &smart, &Wheelchair::velocity_thread); //Sets up sampling frequency of the velosity_thread
t1jain 11:e5823b1f5268 96 queue.call_every(SAMPLEFREQ, &smart, &Wheelchair::assistSafe_thread); //Sets up sampling frequency of the velosity_thread
t1jain 11:e5823b1f5268 97 queue.call_every(SAMPLEFREQ, &smart, &Wheelchair::emergencyButton_thread); //Sets up sampling frequency of the velosity_thread
t1jain 11:e5823b1f5268 98
t1jain 11:e5823b1f5268 99 //queue.call_every(200, rosCom_thread); //Sets up sampling frequency of the velosity_thread
t1jain 11:e5823b1f5268 100
t1jain 11:e5823b1f5268 101 t.reset();
t1jain 11:e5823b1f5268 102 compass.start(callback(&queue, &EventQueue::dispatch_forever)); //Starts running the compass thread
t1jain 11:e5823b1f5268 103 velocity.start(callback(&queue, &EventQueue::dispatch_forever)); //Starts running the velosity thread
t1jain 11:e5823b1f5268 104 assistSafe.start(callback(&queue, &EventQueue::dispatch_forever)); //Starts running the velosity thread
t1jain 11:e5823b1f5268 105 emergencyButton.start(callback(&queue, &EventQueue::dispatch_forever));
t1jain 11:e5823b1f5268 106
t1jain 11:e5823b1f5268 107 //ros_com.start(callback(&queue, &EventQueue::dispatch_forever)); //Starts running the velosity thread
t1jain 11:e5823b1f5268 108 pc.printf("After starting\r\n");
t1jain 11:e5823b1f5268 109
t1jain 11:e5823b1f5268 110 //added
t1jain 11:e5823b1f5268 111 // int emerg_button = e_button;
t1jain 11:e5823b1f5268 112
t1jain 11:e5823b1f5268 113 Watchdog dog;
t1jain 11:e5823b1f5268 114 dog.Configure(watchdogLimit);
t1jain 11:e5823b1f5268 115 pc.printf("Code initiated/reset");
t1jain 11:e5823b1f5268 116
t1jain 11:e5823b1f5268 117 int set = 0;
t1jain 11:e5823b1f5268 118 while(1) {
t1jain 11:e5823b1f5268 119 if( pc.readable()) {
t1jain 11:e5823b1f5268 120 set = 1;
t1jain 11:e5823b1f5268 121 char c = pc.getc(); //Read the instruction sent
t1jain 11:e5823b1f5268 122 if( c == 'w') {
t1jain 11:e5823b1f5268 123 smart.forward(); //Move foward
t1jain 11:e5823b1f5268 124
t1jain 11:e5823b1f5268 125 }
t1jain 11:e5823b1f5268 126 else if( c == 'a') {
t1jain 11:e5823b1f5268 127 smart.left(); //Turn left
t1jain 11:e5823b1f5268 128 }
t1jain 11:e5823b1f5268 129 else if( c == 'd') {
t1jain 11:e5823b1f5268 130 smart.right(); //Turn right
t1jain 11:e5823b1f5268 131 }
t1jain 11:e5823b1f5268 132 else if( c == 's') {
t1jain 11:e5823b1f5268 133 smart.backward(); //Turn rackwards
t1jain 11:e5823b1f5268 134 }
t1jain 11:e5823b1f5268 135 else if( c == 't') {
t1jain 11:e5823b1f5268 136 smart.pid_twistA();
t1jain 11:e5823b1f5268 137 }
t1jain 11:e5823b1f5268 138 else if(c == 'v'){
t1jain 11:e5823b1f5268 139 smart.showOdom();
t1jain 11:e5823b1f5268 140 }
t1jain 11:e5823b1f5268 141 else if(c == 'o') { //Turns on chair
t1jain 11:e5823b1f5268 142 pc.printf("turning on\r\n");
t1jain 11:e5823b1f5268 143 on = 1;
t1jain 11:e5823b1f5268 144 wait(1);
t1jain 11:e5823b1f5268 145 on = 0;
t1jain 11:e5823b1f5268 146 }
t1jain 11:e5823b1f5268 147 else if(c == 'f') { //Turns off chair
t1jain 11:e5823b1f5268 148 pc.printf("turning off\r\n");
t1jain 11:e5823b1f5268 149 off = 1;
t1jain 11:e5823b1f5268 150 wait(1);
t1jain 11:e5823b1f5268 151 off = 0;
t1jain 11:e5823b1f5268 152
t1jain 11:e5823b1f5268 153 }
t1jain 11:e5823b1f5268 154 else if(c == 'k'){ //Sends command to go to the kitchen
t1jain 11:e5823b1f5268 155 smart.pid_twistV();
t1jain 11:e5823b1f5268 156 }
t1jain 11:e5823b1f5268 157 else if( c == 'm' || manual) { //Turns wheelchair to joystick
t1jain 11:e5823b1f5268 158 pc.printf("turning on joystick\r\n");
t1jain 11:e5823b1f5268 159 manual = true;
t1jain 11:e5823b1f5268 160 t.reset();
t1jain 11:e5823b1f5268 161 while(manual) {
t1jain 11:e5823b1f5268 162 smart.move(x,y); //Reads from joystick and moves
t1jain 11:e5823b1f5268 163 if( pc.readable()) {
t1jain 11:e5823b1f5268 164 char d = pc.getc();
t1jain 11:e5823b1f5268 165 if( d == 'm') { //Turns wheelchair from joystick into auto
t1jain 11:e5823b1f5268 166 pc.printf("turning off joystick\r\n");
t1jain 11:e5823b1f5268 167 manual = false;
t1jain 11:e5823b1f5268 168 }
ryanlin97 0:7e6b349182bc 169 }
t1jain 11:e5823b1f5268 170 }
t1jain 11:e5823b1f5268 171 }
t1jain 11:e5823b1f5268 172 else {
jvfausto 9:ca11e4db63a7 173 pc.printf("none \r\n");
jvfausto 9:ca11e4db63a7 174 smart.stop(); //If nothing else is happening stop the chair
t1jain 11:e5823b1f5268 175 }
ryanlin97 0:7e6b349182bc 176 }
t1jain 11:e5823b1f5268 177 else {
t1jain 11:e5823b1f5268 178
t1jain 11:e5823b1f5268 179 smart.stop(); //If nothing else is happening stop the chair
t1jain 11:e5823b1f5268 180 }
t1jain 11:e5823b1f5268 181
t1jain 11:e5823b1f5268 182 wait(process);
t1jain 11:e5823b1f5268 183 dog.Service();
jvfausto 9:ca11e4db63a7 184 }
t1jain 11:e5823b1f5268 185 }