added code to ToF and inserted the e_button

Dependencies:   Version1-4

Dependents:   Version1-4 Version1-3

Committer:
JesiMiranda
Date:
Tue Jul 09 21:25:58 2019 +0000
Revision:
23:497a6a11fe63
Parent:
22:d690ed804636
Child:
25:c9f7d54211bb
finished commenting on the .cpp file and changed the statistics library name

Who changed what in which revision?

UserRevisionLine numberNew contents of line
t1jain 21:b438961f85d5 1 /**************************************************************************
t1jain 21:b438961f85d5 2 * Version 1.0 *
t1jain 21:b438961f85d5 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
JesiMiranda 22:d690ed804636 73 Timer t, IMU_t; // Initialize time object t and IMU timer
t1jain 20:bd257ffdef24 74 EventQueue queue; // Class to organize threads
JesiMiranda 22:d690ed804636 75 Wheelchair smart(xDir,yDir, &pc, &IMU_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
t1jain 21:b438961f85d5 109 Watchdog dog; // Creates Watchdog object
t1jain 21:b438961f85d5 110 dog.Configure(watchdogLimit); // Configures timeout for Watchdog
jvfausto 13:7fe71170d157 111 pc.printf("Code initiated\n");
jvfausto 9:ca11e4db63a7 112 int set = 0;
t1jain 20:bd257ffdef24 113
ryanlin97 0:7e6b349182bc 114 while(1) {
ryanlin97 0:7e6b349182bc 115 if( pc.readable()) {
jvfausto 9:ca11e4db63a7 116 set = 1;
t1jain 21:b438961f85d5 117 char c = pc.getc(); // Read the instruction sent
ryanlin97 0:7e6b349182bc 118 if( c == 'w') {
t1jain 21:b438961f85d5 119 smart.forward(); // Move foward
jvfausto 9:ca11e4db63a7 120
ryanlin97 0:7e6b349182bc 121 }
ryanlin97 0:7e6b349182bc 122 else if( c == 'a') {
t1jain 21:b438961f85d5 123 smart.left(); // Turn left
ryanlin97 0:7e6b349182bc 124 }
jvfausto 9:ca11e4db63a7 125 else if( c == 'd') {
t1jain 21:b438961f85d5 126 smart.right(); // Turn right
ryanlin97 0:7e6b349182bc 127 }
jvfausto 9:ca11e4db63a7 128 else if( c == 's') {
t1jain 21:b438961f85d5 129 smart.backward(); // Turn backwards
jvfausto 9:ca11e4db63a7 130 }
jvfausto 9:ca11e4db63a7 131
jvfausto 9:ca11e4db63a7 132 else if( c == 't') {
jvfausto 9:ca11e4db63a7 133 smart.pid_twistA();
t1jain 21:b438961f85d5 134 }
t1jain 21:b438961f85d5 135
t1jain 21:b438961f85d5 136 else if(c == 'v'){
jvfausto 9:ca11e4db63a7 137 smart.showOdom();
t1jain 21:b438961f85d5 138 }
t1jain 21:b438961f85d5 139
t1jain 21:b438961f85d5 140 else if(c == 'o') { // Turns on chair
jvfausto 7:04f93e6b929f 141 pc.printf("turning on\r\n");
ryanlin97 6:e9b1684a9c00 142 on = 1;
ryanlin97 6:e9b1684a9c00 143 wait(1);
ryanlin97 6:e9b1684a9c00 144 on = 0;
t1jain 21:b438961f85d5 145 }
t1jain 21:b438961f85d5 146
t1jain 21:b438961f85d5 147 else if(c == 'f') { // Turns off chair
jvfausto 7:04f93e6b929f 148 pc.printf("turning off\r\n");
ryanlin97 6:e9b1684a9c00 149 off = 1;
ryanlin97 6:e9b1684a9c00 150 wait(1);
t1jain 21:b438961f85d5 151 off = 0;
t1jain 21:b438961f85d5 152 }
jvfausto 9:ca11e4db63a7 153
t1jain 21:b438961f85d5 154 else if(c == 'k'){ // Sends command to go to the kitchen
jvfausto 9:ca11e4db63a7 155 smart.pid_twistV();
t1jain 21:b438961f85d5 156 }
t1jain 21:b438961f85d5 157
t1jain 21:b438961f85d5 158 else if( c == 'm' || manual) { // Turns wheelchair to joystick
jvfausto 7:04f93e6b929f 159 pc.printf("turning on joystick\r\n");
ryanlin97 0:7e6b349182bc 160 manual = true;
ryanlin97 0:7e6b349182bc 161 t.reset();
ryanlin97 0:7e6b349182bc 162 while(manual) {
t1jain 21:b438961f85d5 163 smart.move(x,y); // Reads from joystick and moves
ryanlin97 0:7e6b349182bc 164 if( pc.readable()) {
ryanlin97 0:7e6b349182bc 165 char d = pc.getc();
t1jain 21:b438961f85d5 166 if( d == 'm') { // Turns wheelchair from joystick into auto
jvfausto 7:04f93e6b929f 167 pc.printf("turning off joystick\r\n");
ryanlin97 0:7e6b349182bc 168 manual = false;
ryanlin97 0:7e6b349182bc 169 }
ryanlin97 0:7e6b349182bc 170 }
jvfausto 7:04f93e6b929f 171 }
ryanlin97 0:7e6b349182bc 172 }
t1jain 21:b438961f85d5 173
t1jain 21:b438961f85d5 174 else {
jvfausto 9:ca11e4db63a7 175 pc.printf("none \r\n");
t1jain 21:b438961f85d5 176 smart.stop(); // If nothing else is happening stop the chair
ryanlin97 0:7e6b349182bc 177 }
ryanlin97 0:7e6b349182bc 178 }
t1jain 21:b438961f85d5 179
t1jain 21:b438961f85d5 180 else {
t1jain 21:b438961f85d5 181 smart.stop(); // If nothing else is happening stop the chair
jvfausto 9:ca11e4db63a7 182 }
jvfausto 11:73b4380d82bf 183
ryanlin97 0:7e6b349182bc 184 wait(process);
jvfausto 11:73b4380d82bf 185
jvfausto 13:7fe71170d157 186 t.stop();
t1jain 20:bd257ffdef24 187 //pc.printf("Time elapsed: %f seconds, Iteration = %d\n", t.read(), iteration);
t1jain 21:b438961f85d5 188 dog.Service(); // Service the Watchdog so it does not cause a system reset - "Kicking"/"Feeding" the dog
jvfausto 13:7fe71170d157 189
ryanlin97 0:7e6b349182bc 190 }
ryanlin97 0:7e6b349182bc 191 }
ryanlin97 0:7e6b349182bc 192