added code to ToF and inserted the e_button

Dependencies:   Version1-4

Dependents:   Version1-4 Version1-3

Committer:
t1jain
Date:
Tue Jul 02 18:53:53 2019 +0000
Revision:
20:bd257ffdef24
Parent:
19:7ead04523f89
Child:
21:b438961f85d5
Pin corrections

Who changed what in which revision?

UserRevisionLine numberNew contents of line
t1jain 20:bd257ffdef24 1 /*************************************************************************
t1jain 20:bd257ffdef24 2 * Version 1.0 *
t1jain 20:bd257ffdef24 3 *************************************************************************/
t1jain 20:bd257ffdef24 4 #include "wheelchair.h"
t1jain 20:bd257ffdef24 5 Serial pc(USBTX, USBRX, 57600); // Serial Monitor
t1jain 19:7ead04523f89 6
t1jain 20:bd257ffdef24 7 /**************************************************************************
t1jain 20:bd257ffdef24 8 * Encoder Pins & Variables *
t1jain 20:bd257ffdef24 9 **************************************************************************/
t1jain 20:bd257ffdef24 10
t1jain 17:770a161346ed 11 QEI wheel (D10, D9, NC, 1200); // Initializes right encoder
t1jain 17:770a161346ed 12 DigitalIn pt3(D10, PullUp); // Pull up resistors to read analog signals into digital signals
jvfausto 9:ca11e4db63a7 13 DigitalIn pt4(D9, PullUp);
jvfausto 9:ca11e4db63a7 14
t1jain 17:770a161346ed 15 QEI wheelS (D7, D8, NC, 1200); // Initializes Left encoder
t1jain 17:770a161346ed 16 DigitalIn pt1(D7, PullUp); // Pull up resistors to read analog signals into digital signals
jvfausto 9:ca11e4db63a7 17 DigitalIn pt2(D8, PullUp);
jvfausto 8:db780b392bae 18
jvfausto 9:ca11e4db63a7 19 int max_velocity;
jvfausto 9:ca11e4db63a7 20
t1jain 20:bd257ffdef24 21 /**************************************************************************
t1jain 20:bd257ffdef24 22 * Variables and Pins for Watchdog and Emergency Button *
t1jain 20:bd257ffdef24 23 **************************************************************************/
t1jain 20:bd257ffdef24 24
t1jain 20:bd257ffdef24 25 DigitalIn e_button(D6, PullDown); // Change to PullUp if testing without Emergency Button Connected
t1jain 20:bd257ffdef24 26 PwmOut on(PE_6); // Turn Wheelchair On
t1jain 20:bd257ffdef24 27 PwmOut off(PE_5); // Turn Wheelchair Off
t1jain 18:94ae9ca5a709 28
t1jain 17:770a161346ed 29 //Watchdog limit should be 0.1; Set to 1 for Testing Only
t1jain 17:770a161346ed 30 double watchdogLimit = 1; // Set timeout limit for watchdog timer in seconds
jvfausto 11:73b4380d82bf 31 int buttonCheck = 0;
jvfausto 11:73b4380d82bf 32 int iteration = 1;
jvfausto 11:73b4380d82bf 33
t1jain 20:bd257ffdef24 34 /**************************************************************************
t1jain 20:bd257ffdef24 35 * Joystick Pins & Variables *
t1jain 20:bd257ffdef24 36 **************************************************************************/
t1jain 20:bd257ffdef24 37
t1jain 20:bd257ffdef24 38 AnalogIn x(A0); // Initializes analog axis for the joystick
t1jain 20:bd257ffdef24 39 AnalogIn y(A1);
t1jain 20:bd257ffdef24 40
t1jain 17:770a161346ed 41 DigitalOut up(D12); // Turn up speed mode for joystick
t1jain 17:770a161346ed 42 DigitalOut down(D13); // Turn down speed mode for joystick
t1jain 17:770a161346ed 43 bool manual = false; // Turns chair joystic to automatic and viceverza
jvfausto 9:ca11e4db63a7 44
t1jain 20:bd257ffdef24 45 /**************************************************************************
t1jain 20:bd257ffdef24 46 * ToF Sensor Pin Assignments *
t1jain 20:bd257ffdef24 47 **************************************************************************/
ryanlin97 0:7e6b349182bc 48
t1jain 20:bd257ffdef24 49 VL53L1X sensor1(PD_13, PD_12, PA_15); // Block 1
t1jain 20:bd257ffdef24 50 VL53L1X sensor2(PD_13, PD_12, PC_7);
jvfausto 10:b4d68db3ddbd 51 VL53L1X sensor3(PD_13, PD_12, PB_5);
t1jain 20:bd257ffdef24 52
t1jain 20:bd257ffdef24 53 VL53L1X sensor4(PD_13, PD_12, PE_11); // Block 2
t1jain 20:bd257ffdef24 54 VL53L1X sensor5(PD_13, PD_12, PF_14);
jvfausto 10:b4d68db3ddbd 55 VL53L1X sensor6(PD_13, PD_12, PE_13);
t1jain 20:bd257ffdef24 56
t1jain 20:bd257ffdef24 57 VL53L1X sensor7(PD_13, PD_12, PG_15); // Block 3
t1jain 20:bd257ffdef24 58 VL53L1X sensor8(PD_13, PD_12, PG_14);
t1jain 20:bd257ffdef24 59 VL53L1X sensor9(PD_13, PD_12, PG_9);
t1jain 20:bd257ffdef24 60
t1jain 20:bd257ffdef24 61 VL53L1X sensor10(PD_13, PD_12, PE_10); // Block 4
t1jain 20:bd257ffdef24 62 VL53L1X sensor11(PD_13, PD_12, PE_12);
t1jain 20:bd257ffdef24 63 VL53L1X sensor12(PD_13, PD_12, PE_14);
ryanlin97 5:90bf5f0d86e9 64
jvfausto 9:ca11e4db63a7 65 VL53L1X* ToF[12] = {&sensor1, &sensor2, &sensor3, &sensor4, &sensor5, &sensor6,
t1jain 20:bd257ffdef24 66 &sensor7, &sensor8, &sensor9, &sensor10, &sensor11, &sensor12}; // Puts ToF sensor pointers into an array
jvfausto 9:ca11e4db63a7 67 VL53L1X** ToFT = ToF;
ryanlin97 0:7e6b349182bc 68
t1jain 20:bd257ffdef24 69 /**************************************************************************
t1jain 20:bd257ffdef24 70 * Thread Definitions *
t1jain 20:bd257ffdef24 71 **************************************************************************/
ryanlin97 0:7e6b349182bc 72
t1jain 20:bd257ffdef24 73 Timer t; // Initialize time object t
t1jain 20:bd257ffdef24 74 EventQueue queue; // Class to organize threads
t1jain 20:bd257ffdef24 75 Wheelchair smart(xDir,yDir, &pc, &t, &wheel, &wheelS, ToFT); // Initialize wheelchair object
t1jain 20:bd257ffdef24 76 Thread compass; // Thread for compass
t1jain 20:bd257ffdef24 77 Thread velocity; // Thread for velocity
t1jain 20:bd257ffdef24 78 Thread ToFSafe; // Thread for safety stuff
t1jain 20:bd257ffdef24 79 Thread emergencyButton; // Thread to check button state and reset device
ryanlin97 5:90bf5f0d86e9 80
t1jain 20:bd257ffdef24 81 /**************************************************************************
t1jain 20:bd257ffdef24 82 * MAIN CODE *
t1jain 20:bd257ffdef24 83 **************************************************************************/
ryanlin97 0:7e6b349182bc 84 int main(void)
t1jain 20:bd257ffdef24 85 {
t1jain 20:bd257ffdef24 86
t1jain 20:bd257ffdef24 87 /* nh.initNode();
jvfausto 9:ca11e4db63a7 88 nh.advertise(chatter);
jvfausto 9:ca11e4db63a7 89 nh.advertise(chatter2);
t1jain 20:bd257ffdef24 90 nh.subscribe(sub); */
t1jain 20:bd257ffdef24 91
t1jain 20:bd257ffdef24 92 pc.printf("Before Starting\r\n");
t1jain 17:770a161346ed 93
t1jain 20:bd257ffdef24 94 //queue.call_every(SAMPLEFREQ, &smart, &Wheelchair::compass_thread); // Sets up sampling frequency of the compass thread
t1jain 20:bd257ffdef24 95 queue.call_every(SAMPLEFREQ, &smart, &Wheelchair::velocity_thread); // Sets up sampling frequency of the velocity thread
t1jain 20:bd257ffdef24 96 queue.call_every(SAMPLEFREQ, &smart, &Wheelchair::ToFSafe_thread); // Sets up sampling frequency of the ToF safety thread
t1jain 20:bd257ffdef24 97 //queue.call_every(200, rosCom_thread); // Sets up sampling frequency of the ROS com thread
t1jain 20:bd257ffdef24 98 queue.call_every(SAMPLEFREQ, &smart, &Wheelchair::emergencyButton_thread); // Sets up sampling frequency of the emergency button thread
t1jain 18:94ae9ca5a709 99
ryanlin97 0:7e6b349182bc 100 t.reset();
t1jain 20:bd257ffdef24 101 //compass.start(callback(&queue, &EventQueue::dispatch_forever)); // Starts running the compass thread
t1jain 20:bd257ffdef24 102 velocity.start(callback(&queue, &EventQueue::dispatch_forever)); // Starts running the velocity thread
t1jain 20:bd257ffdef24 103 ToFSafe.start(callback(&queue, &EventQueue::dispatch_forever)); // Starts running the ROS com thread
t1jain 20:bd257ffdef24 104 //ros_com.start(callback(&queue, &EventQueue::dispatch_forever)); // Starts running the ROS com thread
t1jain 20:bd257ffdef24 105 emergencyButton.start(callback(&queue, &EventQueue::dispatch_forever)); // Starts running the emergency button thread
t1jain 18:94ae9ca5a709 106
t1jain 20:bd257ffdef24 107 pc.printf("After Starting\r\n");
jvfausto 9:ca11e4db63a7 108
jvfausto 9:ca11e4db63a7 109 //added
t1jain 20:bd257ffdef24 110 //int emerg_button = e_button;
jvfausto 13:7fe71170d157 111 Watchdog dog; // Creates Watchdog object
jvfausto 13:7fe71170d157 112 dog.Configure(watchdogLimit); // Configures timeout for Watchdog
jvfausto 13:7fe71170d157 113 pc.printf("Code initiated\n");
jvfausto 9:ca11e4db63a7 114 int set = 0;
t1jain 20:bd257ffdef24 115
ryanlin97 0:7e6b349182bc 116 while(1) {
ryanlin97 0:7e6b349182bc 117 if( pc.readable()) {
jvfausto 9:ca11e4db63a7 118 set = 1;
t1jain 17:770a161346ed 119 char c = pc.getc(); // Read the instruction sent
ryanlin97 0:7e6b349182bc 120 if( c == 'w') {
t1jain 17:770a161346ed 121 smart.forward(); // Move foward
jvfausto 9:ca11e4db63a7 122
ryanlin97 0:7e6b349182bc 123 }
ryanlin97 0:7e6b349182bc 124 else if( c == 'a') {
t1jain 17:770a161346ed 125 smart.left(); // Turn left
ryanlin97 0:7e6b349182bc 126 }
jvfausto 9:ca11e4db63a7 127 else if( c == 'd') {
t1jain 17:770a161346ed 128 smart.right(); // Turn right
ryanlin97 0:7e6b349182bc 129 }
jvfausto 9:ca11e4db63a7 130 else if( c == 's') {
t1jain 17:770a161346ed 131 smart.backward(); // Turn backwards
jvfausto 9:ca11e4db63a7 132 }
jvfausto 9:ca11e4db63a7 133
jvfausto 9:ca11e4db63a7 134 else if( c == 't') {
jvfausto 9:ca11e4db63a7 135 smart.pid_twistA();
jvfausto 9:ca11e4db63a7 136 } else if(c == 'v'){
jvfausto 9:ca11e4db63a7 137 smart.showOdom();
t1jain 17:770a161346ed 138 } else if(c == 'o') { // Turns on chair
jvfausto 7:04f93e6b929f 139 pc.printf("turning on\r\n");
ryanlin97 6:e9b1684a9c00 140 on = 1;
ryanlin97 6:e9b1684a9c00 141 wait(1);
ryanlin97 6:e9b1684a9c00 142 on = 0;
t1jain 17:770a161346ed 143 } else if(c == 'f') { // Turns off chair
jvfausto 7:04f93e6b929f 144 pc.printf("turning off\r\n");
ryanlin97 6:e9b1684a9c00 145 off = 1;
ryanlin97 6:e9b1684a9c00 146 wait(1);
ryanlin97 6:e9b1684a9c00 147 off = 0;
jvfausto 9:ca11e4db63a7 148
t1jain 17:770a161346ed 149 } else if(c == 'k'){ // Sends command to go to the kitchen
jvfausto 9:ca11e4db63a7 150 smart.pid_twistV();
t1jain 17:770a161346ed 151 } else if( c == 'm' || manual) { // Turns wheelchair to joystick
jvfausto 7:04f93e6b929f 152 pc.printf("turning on joystick\r\n");
ryanlin97 0:7e6b349182bc 153 manual = true;
ryanlin97 0:7e6b349182bc 154 t.reset();
ryanlin97 0:7e6b349182bc 155 while(manual) {
t1jain 17:770a161346ed 156 smart.move(x,y); // Reads from joystick and moves
ryanlin97 0:7e6b349182bc 157 if( pc.readable()) {
ryanlin97 0:7e6b349182bc 158 char d = pc.getc();
t1jain 17:770a161346ed 159 if( d == 'm') { // Turns wheelchair from joystick into auto
jvfausto 7:04f93e6b929f 160 pc.printf("turning off joystick\r\n");
ryanlin97 0:7e6b349182bc 161 manual = false;
ryanlin97 0:7e6b349182bc 162 }
ryanlin97 0:7e6b349182bc 163 }
jvfausto 7:04f93e6b929f 164 }
ryanlin97 0:7e6b349182bc 165 }
jvfausto 9:ca11e4db63a7 166 else {
jvfausto 9:ca11e4db63a7 167 pc.printf("none \r\n");
t1jain 17:770a161346ed 168 smart.stop(); // If nothing else is happening stop the chair
ryanlin97 0:7e6b349182bc 169 }
ryanlin97 0:7e6b349182bc 170 }
jvfausto 9:ca11e4db63a7 171 else {
jvfausto 9:ca11e4db63a7 172
t1jain 17:770a161346ed 173 smart.stop(); // If nothing else is happening stop the chair
jvfausto 9:ca11e4db63a7 174 }
jvfausto 11:73b4380d82bf 175
ryanlin97 0:7e6b349182bc 176 wait(process);
jvfausto 11:73b4380d82bf 177
jvfausto 13:7fe71170d157 178 t.stop();
t1jain 20:bd257ffdef24 179 //pc.printf("Time elapsed: %f seconds, Iteration = %d\n", t.read(), iteration);
t1jain 17:770a161346ed 180 dog.Service(); // Service the Watchdog so it does not cause a system reset - "Kicking"/"Feeding" the dog
jvfausto 13:7fe71170d157 181
ryanlin97 0:7e6b349182bc 182 }
ryanlin97 0:7e6b349182bc 183 }
ryanlin97 0:7e6b349182bc 184