Updated with Statistics Library
Dependencies: wheelchaircontrol5
main.cpp@12:43008ddd4a2f, 2019-06-28 (annotated)
- Committer:
- t1jain
- Date:
- Fri Jun 28 23:54:01 2019 +0000
- Revision:
- 12:43008ddd4a2f
- Parent:
- 11:73b4380d82bf
Updated with Statistics Library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
t1jain | 12:43008ddd4a2f | 1 | #include "mbed.h" |
t1jain | 12:43008ddd4a2f | 2 | #include <ros.h> |
t1jain | 12:43008ddd4a2f | 3 | #include <nav_msgs/Odometry.h> // Contains both the twist and pose |
t1jain | 12:43008ddd4a2f | 4 | #include "Watchdog.h" |
ryanlin97 | 0:7e6b349182bc | 5 | #include "wheelchair.h" |
t1jain | 12:43008ddd4a2f | 6 | #include "rtos.h" |
ryanlin97 | 0:7e6b349182bc | 7 | |
t1jain | 12:43008ddd4a2f | 8 | QEI wheel (D10, D9, NC, 1200); // Initializes right encoder |
t1jain | 12:43008ddd4a2f | 9 | DigitalIn pt3(D10, PullUp); // Pull up resistors to read analog signals into digital signals |
jvfausto | 9:ca11e4db63a7 | 10 | DigitalIn pt4(D9, PullUp); |
jvfausto | 9:ca11e4db63a7 | 11 | |
t1jain | 12:43008ddd4a2f | 12 | double watchdogLimit = 1.00; // Set timeout limit for watchdog timer in milliseconds |
jvfausto | 8:db780b392bae | 13 | |
t1jain | 12:43008ddd4a2f | 14 | QEI wheelS (D7, D8, NC, 1200); // Initializes Left encoder |
t1jain | 12:43008ddd4a2f | 15 | DigitalIn pt1(D7, PullUp); // Pull up resistors to read analog signals into digital signals |
jvfausto | 9:ca11e4db63a7 | 16 | DigitalIn pt2(D8, PullUp); |
t1jain | 12:43008ddd4a2f | 17 | //Added |
t1jain | 12:43008ddd4a2f | 18 | DigitalIn e_button(D6, PullDown); |
jvfausto | 8:db780b392bae | 19 | |
jvfausto | 9:ca11e4db63a7 | 20 | int max_velocity; |
jvfausto | 9:ca11e4db63a7 | 21 | //Timer testAccT; |
jvfausto | 9:ca11e4db63a7 | 22 | |
t1jain | 12:43008ddd4a2f | 23 | AnalogIn x(A0); // Initializes analog axis for the joystick |
ryanlin97 | 0:7e6b349182bc | 24 | AnalogIn y(A1); |
ryanlin97 | 0:7e6b349182bc | 25 | |
t1jain | 12:43008ddd4a2f | 26 | DigitalOut up(D12); // Turn up speed mode for joystick |
t1jain | 12:43008ddd4a2f | 27 | DigitalOut down(D13); // Turn down speed mode for joystick |
t1jain | 12:43008ddd4a2f | 28 | PwmOut on(PE_6); // Turn Wheelchair On |
t1jain | 12:43008ddd4a2f | 29 | PwmOut off(PE_5); // Turn Wheelchair Off |
jvfausto | 11:73b4380d82bf | 30 | |
t1jain | 12:43008ddd4a2f | 31 | bool manual = false; // Turns chair joystic to automatic and viceverza |
jvfausto | 9:ca11e4db63a7 | 32 | |
t1jain | 12:43008ddd4a2f | 33 | Serial pc(USBTX, USBRX, 57600); // Serial Monitor |
ryanlin97 | 0:7e6b349182bc | 34 | |
t1jain | 12:43008ddd4a2f | 35 | VL53L1X sensor1(PD_13, PD_12, PA_15); // Initializes ToF sensors |
t1jain | 12:43008ddd4a2f | 36 | VL53L1X sensor2(PD_13, PD_12, PC_7); |
jvfausto | 10:b4d68db3ddbd | 37 | VL53L1X sensor3(PD_13, PD_12, PB_5); |
t1jain | 12:43008ddd4a2f | 38 | VL53L1X sensor4(PD_13, PD_12, PE_11); |
t1jain | 12:43008ddd4a2f | 39 | VL53L1X sensor5(PD_13, PD_12, PF_14); |
jvfausto | 10:b4d68db3ddbd | 40 | VL53L1X sensor6(PD_13, PD_12, PE_13); |
t1jain | 12:43008ddd4a2f | 41 | VL53L1X sensor7(PD_13, PD_12, D1); |
jvfausto | 10:b4d68db3ddbd | 42 | VL53L1X sensor8(PD_13, PD_12, PE_12); |
jvfausto | 10:b4d68db3ddbd | 43 | VL53L1X sensor9(PD_13, PD_12, PE_10); |
jvfausto | 10:b4d68db3ddbd | 44 | VL53L1X sensor10(PD_13, PD_12, PE_15); |
t1jain | 12:43008ddd4a2f | 45 | VL53L1X sensor11(PD_13, PD_12, D1); |
jvfausto | 9:ca11e4db63a7 | 46 | VL53L1X sensor12(PB_11, PB_10, D11); |
ryanlin97 | 5:90bf5f0d86e9 | 47 | |
t1jain | 12:43008ddd4a2f | 48 | VL53L1X* ToF[12] = {&sensor1, &sensor2, &sensor3, &sensor4, &sensor5, &sensor6, |
t1jain | 12:43008ddd4a2f | 49 | &sensor7, &sensor8, &sensor9, &sensor10, &sensor11, &sensor12 |
t1jain | 12:43008ddd4a2f | 50 | }; // Puts ToF sensor pointers into an array |
jvfausto | 9:ca11e4db63a7 | 51 | VL53L1X** ToFT = ToF; |
ryanlin97 | 0:7e6b349182bc | 52 | |
t1jain | 12:43008ddd4a2f | 53 | Timer t; // Initialize time object t |
t1jain | 12:43008ddd4a2f | 54 | EventQueue queue; // Class to organize threads |
jvfausto | 9:ca11e4db63a7 | 55 | Wheelchair smart(xDir,yDir, &pc, &t, &wheel, &wheelS, ToFT); //Initialize wheelchair object |
t1jain | 12:43008ddd4a2f | 56 | Thread compass; // Thread for compass |
t1jain | 12:43008ddd4a2f | 57 | Thread velocity; // Thread for velosity |
t1jain | 12:43008ddd4a2f | 58 | Thread assistSafe; // Thread for safety stuff |
t1jain | 12:43008ddd4a2f | 59 | Thread emergencyButton; // Thread to check button state and reset device |
ryanlin97 | 5:90bf5f0d86e9 | 60 | |
ryanlin97 | 0:7e6b349182bc | 61 | int main(void) |
t1jain | 12:43008ddd4a2f | 62 | { |
t1jain | 12:43008ddd4a2f | 63 | |
t1jain | 12:43008ddd4a2f | 64 | |
t1jain | 12:43008ddd4a2f | 65 | Watchdog dog; // Creates Watchdog object |
t1jain | 12:43008ddd4a2f | 66 | dog.Configure(watchdogLimit); // Configures timeout for Watchdog |
t1jain | 12:43008ddd4a2f | 67 | pc.printf("Code initiated/reset"); |
t1jain | 12:43008ddd4a2f | 68 | |
t1jain | 12:43008ddd4a2f | 69 | /* nh.initNode(); |
t1jain | 12:43008ddd4a2f | 70 | nh.advertise(chatter); |
t1jain | 12:43008ddd4a2f | 71 | nh.advertise(chatter2); |
t1jain | 12:43008ddd4a2f | 72 | nh.subscribe(sub);*/ |
jvfausto | 9:ca11e4db63a7 | 73 | //testAccT.start(); |
jvfausto | 9:ca11e4db63a7 | 74 | pc.printf("before starting\r\n"); |
t1jain | 12:43008ddd4a2f | 75 | queue.call_every(SAMPLEFREQ, &smart, &Wheelchair::compass_thread); // Sets up sampling frequency of the compass_thread |
t1jain | 12:43008ddd4a2f | 76 | queue.call_every(SAMPLEFREQ, &smart, &Wheelchair::velocity_thread); // Sets up sampling frequency of the velocity_thread |
t1jain | 12:43008ddd4a2f | 77 | queue.call_every(SAMPLEFREQ, &smart, &Wheelchair::assistSafe_thread); // Sets up sampling frequency of the velosity_thread |
t1jain | 12:43008ddd4a2f | 78 | queue.call_every(SAMPLEFREQ, &smart, &Wheelchair::emergencyButton_thread); // Sets up sampling frequency of the emergencyButton_thread |
jvfausto | 9:ca11e4db63a7 | 79 | //queue.call_every(200, rosCom_thread); //Sets up sampling frequency of the velosity_thread |
ryanlin97 | 0:7e6b349182bc | 80 | t.reset(); |
t1jain | 12:43008ddd4a2f | 81 | compass.start(callback(&queue, &EventQueue::dispatch_forever)); // Starts running the compass thread |
t1jain | 12:43008ddd4a2f | 82 | velocity.start(callback(&queue, &EventQueue::dispatch_forever)); // Starts running the velosity thread |
t1jain | 12:43008ddd4a2f | 83 | assistSafe.start(callback(&queue, &EventQueue::dispatch_forever)); // Starts running the velosity thread |
t1jain | 12:43008ddd4a2f | 84 | emergencyButton.start(callback(&queue, &EventQueue::dispatch_forever)); |
t1jain | 12:43008ddd4a2f | 85 | |
t1jain | 12:43008ddd4a2f | 86 | //ros_com.start(callback(&queue, &EventQueue::dispatch_forever)); // Starts running the velosity thread |
t1jain | 12:43008ddd4a2f | 87 | pc.printf("After starting\r\n"); |
jvfausto | 9:ca11e4db63a7 | 88 | |
jvfausto | 9:ca11e4db63a7 | 89 | int set = 0; |
ryanlin97 | 0:7e6b349182bc | 90 | while(1) { |
ryanlin97 | 0:7e6b349182bc | 91 | if( pc.readable()) { |
jvfausto | 9:ca11e4db63a7 | 92 | set = 1; |
t1jain | 12:43008ddd4a2f | 93 | char c = pc.getc(); // Read the instruction sent |
ryanlin97 | 0:7e6b349182bc | 94 | if( c == 'w') { |
t1jain | 12:43008ddd4a2f | 95 | smart.forward(); // Move foward |
jvfausto | 9:ca11e4db63a7 | 96 | |
t1jain | 12:43008ddd4a2f | 97 | } else if( c == 'a') { |
t1jain | 12:43008ddd4a2f | 98 | smart.left(); // Turn left |
t1jain | 12:43008ddd4a2f | 99 | } else if( c == 'd') { |
t1jain | 12:43008ddd4a2f | 100 | smart.right(); // Turn right |
t1jain | 12:43008ddd4a2f | 101 | } else if( c == 's') { |
t1jain | 12:43008ddd4a2f | 102 | smart.backward(); // Turn rackwards |
ryanlin97 | 0:7e6b349182bc | 103 | } |
t1jain | 12:43008ddd4a2f | 104 | |
t1jain | 12:43008ddd4a2f | 105 | else if( c == 't') { |
jvfausto | 9:ca11e4db63a7 | 106 | smart.pid_twistA(); |
t1jain | 12:43008ddd4a2f | 107 | } else if(c == 'v') { |
jvfausto | 9:ca11e4db63a7 | 108 | smart.showOdom(); |
t1jain | 12:43008ddd4a2f | 109 | } else if(c == 'o') { // Turns on chair |
jvfausto | 7:04f93e6b929f | 110 | pc.printf("turning on\r\n"); |
ryanlin97 | 6:e9b1684a9c00 | 111 | on = 1; |
ryanlin97 | 6:e9b1684a9c00 | 112 | wait(1); |
ryanlin97 | 6:e9b1684a9c00 | 113 | on = 0; |
t1jain | 12:43008ddd4a2f | 114 | } else if(c == 'f') { // Turns off chair |
jvfausto | 7:04f93e6b929f | 115 | pc.printf("turning off\r\n"); |
ryanlin97 | 6:e9b1684a9c00 | 116 | off = 1; |
ryanlin97 | 6:e9b1684a9c00 | 117 | wait(1); |
ryanlin97 | 6:e9b1684a9c00 | 118 | off = 0; |
t1jain | 12:43008ddd4a2f | 119 | |
t1jain | 12:43008ddd4a2f | 120 | } else if(c == 'k') { // Sends command to go to the kitchen |
jvfausto | 9:ca11e4db63a7 | 121 | smart.pid_twistV(); |
t1jain | 12:43008ddd4a2f | 122 | } else if( c == 'm' || manual) { // Turns wheelchair to joystick |
jvfausto | 7:04f93e6b929f | 123 | pc.printf("turning on joystick\r\n"); |
ryanlin97 | 0:7e6b349182bc | 124 | manual = true; |
ryanlin97 | 0:7e6b349182bc | 125 | t.reset(); |
ryanlin97 | 0:7e6b349182bc | 126 | while(manual) { |
t1jain | 12:43008ddd4a2f | 127 | smart.move(x,y); // Reads from joystick and moves |
ryanlin97 | 0:7e6b349182bc | 128 | if( pc.readable()) { |
ryanlin97 | 0:7e6b349182bc | 129 | char d = pc.getc(); |
t1jain | 12:43008ddd4a2f | 130 | if( d == 'm') { // Turns wheelchair from joystick into auto |
jvfausto | 7:04f93e6b929f | 131 | pc.printf("turning off joystick\r\n"); |
ryanlin97 | 0:7e6b349182bc | 132 | manual = false; |
ryanlin97 | 0:7e6b349182bc | 133 | } |
ryanlin97 | 0:7e6b349182bc | 134 | } |
t1jain | 12:43008ddd4a2f | 135 | } |
t1jain | 12:43008ddd4a2f | 136 | } else { |
t1jain | 12:43008ddd4a2f | 137 | pc.printf("none \r\n"); |
t1jain | 12:43008ddd4a2f | 138 | smart.stop(); // If nothing else is happening stop the chair |
ryanlin97 | 0:7e6b349182bc | 139 | } |
t1jain | 12:43008ddd4a2f | 140 | } else { |
t1jain | 12:43008ddd4a2f | 141 | |
t1jain | 12:43008ddd4a2f | 142 | smart.stop(); // If nothing else is happening stop the chair |
ryanlin97 | 0:7e6b349182bc | 143 | } |
t1jain | 12:43008ddd4a2f | 144 | |
ryanlin97 | 0:7e6b349182bc | 145 | wait(process); |
jvfausto | 11:73b4380d82bf | 146 | dog.Service(); // Service the Watchdog so it does not cause a system reset - "Kicking"/"Feeding" the dog |
ryanlin97 | 0:7e6b349182bc | 147 | } |
ryanlin97 | 0:7e6b349182bc | 148 | } |
ryanlin97 | 0:7e6b349182bc | 149 |