test program
Dependencies: LSM9DS1_Library2 MotorV2 SRF05 Tracker2 mbed nRF24L01P
Fork of DUMP_TRUCK_SPR2017 by
DumpTruck.cpp@2:7811df5a6052, 2016-11-15 (annotated)
- Committer:
- Josahty
- Date:
- Tue Nov 15 20:04:54 2016 +0000
- Revision:
- 2:7811df5a6052
- Parent:
- 1:90b640c90506
- Child:
- 3:cd6e2d7c7c9c
corrected drive function
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Josahty | 0:6942f0e2198c | 1 | #include "DumpTruck.h" |
Josahty | 0:6942f0e2198c | 2 | #include "stdlib.h" |
Josahty | 0:6942f0e2198c | 3 | |
Josahty | 0:6942f0e2198c | 4 | Serial pc(USBTX, USBRX); |
Josahty | 0:6942f0e2198c | 5 | |
Josahty | 0:6942f0e2198c | 6 | DumpTruck::DumpTruck(int truckId) : truckNumber(truckId) { |
Josahty | 0:6942f0e2198c | 7 | truckNumber = truckId; |
Josahty | 0:6942f0e2198c | 8 | } |
Josahty | 0:6942f0e2198c | 9 | |
Josahty | 0:6942f0e2198c | 10 | PwmOut DrivePin(p24); |
Josahty | 0:6942f0e2198c | 11 | DigitalOut DirPin(p30); |
Josahty | 2:7811df5a6052 | 12 | int travelled; |
Josahty | 0:6942f0e2198c | 13 | |
Josahty | 0:6942f0e2198c | 14 | |
Josahty | 0:6942f0e2198c | 15 | void Calibrate(){} |
Josahty | 0:6942f0e2198c | 16 | |
Josahty | 0:6942f0e2198c | 17 | void DumpTruck::drive() { |
Josahty | 0:6942f0e2198c | 18 | // get desired distance and speed |
Josahty | 0:6942f0e2198c | 19 | distance = 0; // placeholder |
Josahty | 0:6942f0e2198c | 20 | // speed = 0.5; |
Josahty | 0:6942f0e2198c | 21 | // distance = setTruckDistance(); |
Josahty | 0:6942f0e2198c | 22 | speed = setTruckSpeed(); |
Josahty | 0:6942f0e2198c | 23 | // terminal confirmation of distance and speed |
Josahty | 0:6942f0e2198c | 24 | pc.printf("Truck will drive a distance of %f at a speed of %f\r\n",distance,speed); |
Josahty | 0:6942f0e2198c | 25 | // insert while loop here which relates to distance travelled |
Josahty | 0:6942f0e2198c | 26 | while(1) { |
Josahty | 0:6942f0e2198c | 27 | // if the speed is greater than zero, go forward at specified speed |
Josahty | 0:6942f0e2198c | 28 | if (speed >= 0) { |
Josahty | 0:6942f0e2198c | 29 | DirPin = 1; |
Josahty | 0:6942f0e2198c | 30 | DrivePin = speed; |
Josahty | 0:6942f0e2198c | 31 | } // else, go backwards at specified speed |
Josahty | 0:6942f0e2198c | 32 | else { |
Josahty | 0:6942f0e2198c | 33 | DirPin = 0; |
Josahty | 2:7811df5a6052 | 34 | DrivePin = -1*speed; |
Josahty | 0:6942f0e2198c | 35 | } |
Josahty | 0:6942f0e2198c | 36 | } |
Josahty | 0:6942f0e2198c | 37 | // stop motors |
Josahty | 0:6942f0e2198c | 38 | // stop(); |
Josahty | 0:6942f0e2198c | 39 | // calibrate motors |
Josahty | 0:6942f0e2198c | 40 | Calibrate(); |
Josahty | 0:6942f0e2198c | 41 | // |
Josahty | 2:7811df5a6052 | 42 | travelled = 0; |
Josahty | 0:6942f0e2198c | 43 | } |
Josahty | 0:6942f0e2198c | 44 | |
Josahty | 0:6942f0e2198c | 45 | //float DumpTruck::setTruckDistance(){ |
Josahty | 0:6942f0e2198c | 46 | // // terminal prompt for distance |
Josahty | 0:6942f0e2198c | 47 | // pc.printf("Please enter the truck distance:"); |
Josahty | 0:6942f0e2198c | 48 | // // sets return value to given value |
Josahty | 0:6942f0e2198c | 49 | // float input = pc.getc(); |
Josahty | 0:6942f0e2198c | 50 | // // returns value |
Josahty | 0:6942f0e2198c | 51 | // return input; |
Josahty | 0:6942f0e2198c | 52 | // } |
Josahty | 0:6942f0e2198c | 53 | // |
Josahty | 0:6942f0e2198c | 54 | float DumpTruck::setTruckSpeed(){ |
Josahty | 2:7811df5a6052 | 55 | float mod = 0; |
Josahty | 0:6942f0e2198c | 56 | float div = 10; |
Josahty | 2:7811df5a6052 | 57 | pc.printf("Enter a direction - 1 = forward, 0 = backward:\r\n"); |
Josahty | 2:7811df5a6052 | 58 | float dir = pc.getc(); |
Josahty | 2:7811df5a6052 | 59 | while ((dir<48)||(dir>49)){ |
Josahty | 2:7811df5a6052 | 60 | pc.printf("Invalid input. Please enter a valid direction, 1 or 0: \r\n"); |
Josahty | 2:7811df5a6052 | 61 | dir = pc.getc(); |
Josahty | 2:7811df5a6052 | 62 | } |
Josahty | 2:7811df5a6052 | 63 | if (dir == 48) { |
Josahty | 2:7811df5a6052 | 64 | mod = -1; } |
Josahty | 2:7811df5a6052 | 65 | else { |
Josahty | 2:7811df5a6052 | 66 | mod = 1; } |
Josahty | 2:7811df5a6052 | 67 | |
Josahty | 0:6942f0e2198c | 68 | // terminal prompt for speed |
Josahty | 2:7811df5a6052 | 69 | pc.printf("Please enter a truck speed from 0-9 or F for full speed:\r\n"); |
Josahty | 0:6942f0e2198c | 70 | // sets return value to given value |
Josahty | 0:6942f0e2198c | 71 | float input = pc.getc(); |
Josahty | 1:90b640c90506 | 72 | while ((input<48)||(input>57)){ |
Josahty | 2:7811df5a6052 | 73 | if((input == 70) || (input == 102)) { |
Josahty | 2:7811df5a6052 | 74 | input = 1; |
Josahty | 2:7811df5a6052 | 75 | input = input * mod; |
Josahty | 2:7811df5a6052 | 76 | return input; |
Josahty | 2:7811df5a6052 | 77 | } |
Josahty | 2:7811df5a6052 | 78 | pc.printf("Invalid input. Please enter a valid speed from 0-9 or F: \r\n"); |
Josahty | 1:90b640c90506 | 79 | input = pc.getc(); |
Josahty | 0:6942f0e2198c | 80 | } |
Josahty | 0:6942f0e2198c | 81 | input = input - 48; |
Josahty | 0:6942f0e2198c | 82 | input = input * (1/div); |
Josahty | 0:6942f0e2198c | 83 | // returns value |
Josahty | 2:7811df5a6052 | 84 | input = input * mod; |
Josahty | 0:6942f0e2198c | 85 | return input;} |
Josahty | 0:6942f0e2198c | 86 | |
Josahty | 0:6942f0e2198c | 87 | |
Josahty | 0:6942f0e2198c | 88 | void DumpTruck::stop(){ |
Josahty | 2:7811df5a6052 | 89 | pc.printf("Truck will now stop.\r\n"); |
Josahty | 0:6942f0e2198c | 90 | //stop motors |
Josahty | 0:6942f0e2198c | 91 | DrivePin = 0; |
Josahty | 0:6942f0e2198c | 92 | } |
Josahty | 0:6942f0e2198c | 93 | |
Josahty | 0:6942f0e2198c | 94 | //void DumpTruck::RotateTo() { |
Josahty | 0:6942f0e2198c | 95 | // pc.printf("Desired Angle: "); |
Josahty | 0:6942f0e2198c | 96 | // DesiredPivotAngle = pc.getc(); |
Josahty | 0:6942f0e2198c | 97 | // float CurrentPivotAngle = getPivotAngle(); |
Josahty | 0:6942f0e2198c | 98 | // Difference = DesiredPivotAngle-CurrentPivotAngle; |
Josahty | 0:6942f0e2198c | 99 | // if (Difference > limit) |
Josahty | 0:6942f0e2198c | 100 | // { |
Josahty | 0:6942f0e2198c | 101 | // PivotSpeed = setPivotSpeed(); |
Josahty | 0:6942f0e2198c | 102 | // pc.printf("Rotating to %s /n", DesiredPivotAngle); |
Josahty | 0:6942f0e2198c | 103 | // //Drive Motors |
Josahty | 0:6942f0e2198c | 104 | // } |
Josahty | 0:6942f0e2198c | 105 | // else |
Josahty | 0:6942f0e2198c | 106 | // {stop()} |
Josahty | 0:6942f0e2198c | 107 | // |
Josahty | 0:6942f0e2198c | 108 | //} |
Josahty | 0:6942f0e2198c | 109 | // |
Josahty | 0:6942f0e2198c | 110 | //void DumpTruck::BedDown() { |
Josahty | 0:6942f0e2198c | 111 | // SwitchState = bool GetBedUpperSwitch(); |
Josahty | 0:6942f0e2198c | 112 | // if SwitchState |
Josahty | 0:6942f0e2198c | 113 | // {return 0;} |
Josahty | 0:6942f0e2198c | 114 | // else |
Josahty | 0:6942f0e2198c | 115 | // |
Josahty | 0:6942f0e2198c | 116 | // while(SwitchState == 0) |
Josahty | 0:6942f0e2198c | 117 | // { |
Josahty | 0:6942f0e2198c | 118 | // //driveMotor |
Josahty | 0:6942f0e2198c | 119 | // } |
Josahty | 0:6942f0e2198c | 120 | // calibrate(); |
Josahty | 0:6942f0e2198c | 121 | // } |
Josahty | 0:6942f0e2198c | 122 | //void DumpTruck::LowerBed() { |
Josahty | 0:6942f0e2198c | 123 | // SwitchState = bool GetLowerUpperSwitch(); |
Josahty | 0:6942f0e2198c | 124 | // if SwitchState |
Josahty | 0:6942f0e2198c | 125 | // {return 0;} |
Josahty | 0:6942f0e2198c | 126 | // else |
Josahty | 0:6942f0e2198c | 127 | // |
Josahty | 0:6942f0e2198c | 128 | // while(SwitchState == 0) |
Josahty | 0:6942f0e2198c | 129 | // { |
Josahty | 0:6942f0e2198c | 130 | // //driveMotor |
Josahty | 0:6942f0e2198c | 131 | // } |
Josahty | 0:6942f0e2198c | 132 | // calibrate(); |
Josahty | 0:6942f0e2198c | 133 | // } |
Josahty | 0:6942f0e2198c | 134 | //} |
Josahty | 0:6942f0e2198c | 135 | //void DumpTruck::raiseTo(int angle) { |
Josahty | 0:6942f0e2198c | 136 | // pc.printf("Desired Angle: "); |
Josahty | 0:6942f0e2198c | 137 | // DesiredBedAngle = pc.getc(); |
Josahty | 0:6942f0e2198c | 138 | // float CurrentBedAngle = getBedAngle(); |
Josahty | 0:6942f0e2198c | 139 | // Difference = DesiredBedAngle-CurrentBedAngle; |
Josahty | 0:6942f0e2198c | 140 | // if (Difference > limit) |
Josahty | 0:6942f0e2198c | 141 | // { |
Josahty | 0:6942f0e2198c | 142 | // BedSpeed = setBedSpeed(); |
Josahty | 0:6942f0e2198c | 143 | // pc.printf("Rotating to %s /n", DesiredBedAngle); |
Josahty | 0:6942f0e2198c | 144 | // //Drive Motors |
Josahty | 0:6942f0e2198c | 145 | // } |
Josahty | 0:6942f0e2198c | 146 | // else |
Josahty | 0:6942f0e2198c | 147 | // {stop()} |
Josahty | 0:6942f0e2198c | 148 | //} |
Josahty | 0:6942f0e2198c | 149 | // |
Josahty | 0:6942f0e2198c | 150 | //float DumpTruck::getPivotAngle(){ |
Josahty | 0:6942f0e2198c | 151 | // float input = AnalogIn(x); |
Josahty | 0:6942f0e2198c | 152 | // return input |
Josahty | 0:6942f0e2198c | 153 | // } |
Josahty | 0:6942f0e2198c | 154 | //float DumpTruck::setPivotSpeed(){ |
Josahty | 0:6942f0e2198c | 155 | // pc.printf("Please enter the Pivot speed /n"); |
Josahty | 0:6942f0e2198c | 156 | // float input = pc.getc(); |
Josahty | 0:6942f0e2198c | 157 | // return input; |
Josahty | 0:6942f0e2198c | 158 | // } |
Josahty | 0:6942f0e2198c | 159 | // |
Josahty | 0:6942f0e2198c | 160 | //float DumpTruck::setBedSpeed(){ |
Josahty | 0:6942f0e2198c | 161 | // pc.printf("Please enter the Bed speed /n"); |
Josahty | 0:6942f0e2198c | 162 | // float input = pc.getc(); |
Josahty | 0:6942f0e2198c | 163 | // return input; |
Josahty | 0:6942f0e2198c | 164 | // } |
Josahty | 0:6942f0e2198c | 165 | //float DumpTruck::getBedAngle(){ |
Josahty | 0:6942f0e2198c | 166 | // float input = AnalogIn(x); // data from potentiometer |
Josahty | 0:6942f0e2198c | 167 | // return input; |
Josahty | 0:6942f0e2198c | 168 | // } |
Josahty | 0:6942f0e2198c | 169 | // |
Josahty | 0:6942f0e2198c | 170 |