![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
added code to ToF and inserted the e_button
Dependencies: Version1-4
Dependents: Version1-4 Version1-3
main.cpp@21:b438961f85d5, 2019-07-02 (annotated)
- Committer:
- t1jain
- Date:
- Tue Jul 02 19:19:28 2019 +0000
- Revision:
- 21:b438961f85d5
- Parent:
- 20:bd257ffdef24
- Child:
- 22:d690ed804636
Comments added
Who changed what in which revision?
User | Revision | Line number | New 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 | |
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; |
t1jain | 21:b438961f85d5 | 111 | Watchdog dog; // Creates Watchdog object |
t1jain | 21:b438961f85d5 | 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 | 21:b438961f85d5 | 119 | char c = pc.getc(); // Read the instruction sent |
ryanlin97 | 0:7e6b349182bc | 120 | if( c == 'w') { |
t1jain | 21:b438961f85d5 | 121 | smart.forward(); // Move foward |
jvfausto | 9:ca11e4db63a7 | 122 | |
ryanlin97 | 0:7e6b349182bc | 123 | } |
ryanlin97 | 0:7e6b349182bc | 124 | else if( c == 'a') { |
t1jain | 21:b438961f85d5 | 125 | smart.left(); // Turn left |
ryanlin97 | 0:7e6b349182bc | 126 | } |
jvfausto | 9:ca11e4db63a7 | 127 | else if( c == 'd') { |
t1jain | 21:b438961f85d5 | 128 | smart.right(); // Turn right |
ryanlin97 | 0:7e6b349182bc | 129 | } |
jvfausto | 9:ca11e4db63a7 | 130 | else if( c == 's') { |
t1jain | 21:b438961f85d5 | 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(); |
t1jain | 21:b438961f85d5 | 136 | } |
t1jain | 21:b438961f85d5 | 137 | |
t1jain | 21:b438961f85d5 | 138 | else if(c == 'v'){ |
jvfausto | 9:ca11e4db63a7 | 139 | smart.showOdom(); |
t1jain | 21:b438961f85d5 | 140 | } |
t1jain | 21:b438961f85d5 | 141 | |
t1jain | 21:b438961f85d5 | 142 | else if(c == 'o') { // Turns on chair |
jvfausto | 7:04f93e6b929f | 143 | pc.printf("turning on\r\n"); |
ryanlin97 | 6:e9b1684a9c00 | 144 | on = 1; |
ryanlin97 | 6:e9b1684a9c00 | 145 | wait(1); |
ryanlin97 | 6:e9b1684a9c00 | 146 | on = 0; |
t1jain | 21:b438961f85d5 | 147 | } |
t1jain | 21:b438961f85d5 | 148 | |
t1jain | 21:b438961f85d5 | 149 | else if(c == 'f') { // Turns off chair |
jvfausto | 7:04f93e6b929f | 150 | pc.printf("turning off\r\n"); |
ryanlin97 | 6:e9b1684a9c00 | 151 | off = 1; |
ryanlin97 | 6:e9b1684a9c00 | 152 | wait(1); |
t1jain | 21:b438961f85d5 | 153 | off = 0; |
t1jain | 21:b438961f85d5 | 154 | } |
jvfausto | 9:ca11e4db63a7 | 155 | |
t1jain | 21:b438961f85d5 | 156 | else if(c == 'k'){ // Sends command to go to the kitchen |
jvfausto | 9:ca11e4db63a7 | 157 | smart.pid_twistV(); |
t1jain | 21:b438961f85d5 | 158 | } |
t1jain | 21:b438961f85d5 | 159 | |
t1jain | 21:b438961f85d5 | 160 | else if( c == 'm' || manual) { // Turns wheelchair to joystick |
jvfausto | 7:04f93e6b929f | 161 | pc.printf("turning on joystick\r\n"); |
ryanlin97 | 0:7e6b349182bc | 162 | manual = true; |
ryanlin97 | 0:7e6b349182bc | 163 | t.reset(); |
ryanlin97 | 0:7e6b349182bc | 164 | while(manual) { |
t1jain | 21:b438961f85d5 | 165 | smart.move(x,y); // Reads from joystick and moves |
ryanlin97 | 0:7e6b349182bc | 166 | if( pc.readable()) { |
ryanlin97 | 0:7e6b349182bc | 167 | char d = pc.getc(); |
t1jain | 21:b438961f85d5 | 168 | if( d == 'm') { // Turns wheelchair from joystick into auto |
jvfausto | 7:04f93e6b929f | 169 | pc.printf("turning off joystick\r\n"); |
ryanlin97 | 0:7e6b349182bc | 170 | manual = false; |
ryanlin97 | 0:7e6b349182bc | 171 | } |
ryanlin97 | 0:7e6b349182bc | 172 | } |
jvfausto | 7:04f93e6b929f | 173 | } |
ryanlin97 | 0:7e6b349182bc | 174 | } |
t1jain | 21:b438961f85d5 | 175 | |
t1jain | 21:b438961f85d5 | 176 | else { |
jvfausto | 9:ca11e4db63a7 | 177 | pc.printf("none \r\n"); |
t1jain | 21:b438961f85d5 | 178 | smart.stop(); // If nothing else is happening stop the chair |
ryanlin97 | 0:7e6b349182bc | 179 | } |
ryanlin97 | 0:7e6b349182bc | 180 | } |
t1jain | 21:b438961f85d5 | 181 | |
t1jain | 21:b438961f85d5 | 182 | else { |
t1jain | 21:b438961f85d5 | 183 | smart.stop(); // If nothing else is happening stop the chair |
jvfausto | 9:ca11e4db63a7 | 184 | } |
jvfausto | 11:73b4380d82bf | 185 | |
ryanlin97 | 0:7e6b349182bc | 186 | wait(process); |
jvfausto | 11:73b4380d82bf | 187 | |
jvfausto | 13:7fe71170d157 | 188 | t.stop(); |
t1jain | 20:bd257ffdef24 | 189 | //pc.printf("Time elapsed: %f seconds, Iteration = %d\n", t.read(), iteration); |
t1jain | 21:b438961f85d5 | 190 | dog.Service(); // Service the Watchdog so it does not cause a system reset - "Kicking"/"Feeding" the dog |
jvfausto | 13:7fe71170d157 | 191 | |
ryanlin97 | 0:7e6b349182bc | 192 | } |
ryanlin97 | 0:7e6b349182bc | 193 | } |
ryanlin97 | 0:7e6b349182bc | 194 |