test program

Dependencies:   LSM9DS1_Library2 MotorV2 SRF05 Tracker2 mbed nRF24L01P

Fork of DUMP_TRUCK_SPR2017 by Terrabots

Committer:
Josahty
Date:
Tue Nov 01 20:19:20 2016 +0000
Revision:
1:90b640c90506
Parent:
0:6942f0e2198c
Child:
2:7811df5a6052
new version, added working error catch to drive()

Who changed what in which revision?

UserRevisionLine numberNew 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 0:6942f0e2198c 12 int i;
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 0:6942f0e2198c 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 0:6942f0e2198c 42 i = 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 0:6942f0e2198c 55
Josahty 0:6942f0e2198c 56 float div = 10;
Josahty 0:6942f0e2198c 57 // terminal prompt for speed
Josahty 0:6942f0e2198c 58 pc.printf("Please enter the truck speed: \n");
Josahty 0:6942f0e2198c 59 // sets return value to given value
Josahty 0:6942f0e2198c 60 float input = pc.getc();
Josahty 1:90b640c90506 61 while ((input<48)||(input>57)){
Josahty 0:6942f0e2198c 62 pc.printf("Fix your input sir: \r\n");
Josahty 1:90b640c90506 63 input = pc.getc();
Josahty 0:6942f0e2198c 64 }
Josahty 0:6942f0e2198c 65 input = input - 48;
Josahty 0:6942f0e2198c 66 input = input * (1/div);
Josahty 0:6942f0e2198c 67 // returns value
Josahty 0:6942f0e2198c 68 return input;}
Josahty 0:6942f0e2198c 69
Josahty 0:6942f0e2198c 70
Josahty 0:6942f0e2198c 71 void DumpTruck::stop(){
Josahty 0:6942f0e2198c 72 pc.printf("Truck will now stop.");
Josahty 0:6942f0e2198c 73 //stop motors
Josahty 0:6942f0e2198c 74 DrivePin = 0;
Josahty 0:6942f0e2198c 75 }
Josahty 0:6942f0e2198c 76
Josahty 0:6942f0e2198c 77 //void DumpTruck::RotateTo() {
Josahty 0:6942f0e2198c 78 // pc.printf("Desired Angle: ");
Josahty 0:6942f0e2198c 79 // DesiredPivotAngle = pc.getc();
Josahty 0:6942f0e2198c 80 // float CurrentPivotAngle = getPivotAngle();
Josahty 0:6942f0e2198c 81 // Difference = DesiredPivotAngle-CurrentPivotAngle;
Josahty 0:6942f0e2198c 82 // if (Difference > limit)
Josahty 0:6942f0e2198c 83 // {
Josahty 0:6942f0e2198c 84 // PivotSpeed = setPivotSpeed();
Josahty 0:6942f0e2198c 85 // pc.printf("Rotating to %s /n", DesiredPivotAngle);
Josahty 0:6942f0e2198c 86 // //Drive Motors
Josahty 0:6942f0e2198c 87 // }
Josahty 0:6942f0e2198c 88 // else
Josahty 0:6942f0e2198c 89 // {stop()}
Josahty 0:6942f0e2198c 90 //
Josahty 0:6942f0e2198c 91 //}
Josahty 0:6942f0e2198c 92 //
Josahty 0:6942f0e2198c 93 //void DumpTruck::BedDown() {
Josahty 0:6942f0e2198c 94 // SwitchState = bool GetBedUpperSwitch();
Josahty 0:6942f0e2198c 95 // if SwitchState
Josahty 0:6942f0e2198c 96 // {return 0;}
Josahty 0:6942f0e2198c 97 // else
Josahty 0:6942f0e2198c 98 //
Josahty 0:6942f0e2198c 99 // while(SwitchState == 0)
Josahty 0:6942f0e2198c 100 // {
Josahty 0:6942f0e2198c 101 // //driveMotor
Josahty 0:6942f0e2198c 102 // }
Josahty 0:6942f0e2198c 103 // calibrate();
Josahty 0:6942f0e2198c 104 // }
Josahty 0:6942f0e2198c 105 //void DumpTruck::LowerBed() {
Josahty 0:6942f0e2198c 106 // SwitchState = bool GetLowerUpperSwitch();
Josahty 0:6942f0e2198c 107 // if SwitchState
Josahty 0:6942f0e2198c 108 // {return 0;}
Josahty 0:6942f0e2198c 109 // else
Josahty 0:6942f0e2198c 110 //
Josahty 0:6942f0e2198c 111 // while(SwitchState == 0)
Josahty 0:6942f0e2198c 112 // {
Josahty 0:6942f0e2198c 113 // //driveMotor
Josahty 0:6942f0e2198c 114 // }
Josahty 0:6942f0e2198c 115 // calibrate();
Josahty 0:6942f0e2198c 116 // }
Josahty 0:6942f0e2198c 117 //}
Josahty 0:6942f0e2198c 118 //void DumpTruck::raiseTo(int angle) {
Josahty 0:6942f0e2198c 119 // pc.printf("Desired Angle: ");
Josahty 0:6942f0e2198c 120 // DesiredBedAngle = pc.getc();
Josahty 0:6942f0e2198c 121 // float CurrentBedAngle = getBedAngle();
Josahty 0:6942f0e2198c 122 // Difference = DesiredBedAngle-CurrentBedAngle;
Josahty 0:6942f0e2198c 123 // if (Difference > limit)
Josahty 0:6942f0e2198c 124 // {
Josahty 0:6942f0e2198c 125 // BedSpeed = setBedSpeed();
Josahty 0:6942f0e2198c 126 // pc.printf("Rotating to %s /n", DesiredBedAngle);
Josahty 0:6942f0e2198c 127 // //Drive Motors
Josahty 0:6942f0e2198c 128 // }
Josahty 0:6942f0e2198c 129 // else
Josahty 0:6942f0e2198c 130 // {stop()}
Josahty 0:6942f0e2198c 131 //}
Josahty 0:6942f0e2198c 132 //
Josahty 0:6942f0e2198c 133 //float DumpTruck::getPivotAngle(){
Josahty 0:6942f0e2198c 134 // float input = AnalogIn(x);
Josahty 0:6942f0e2198c 135 // return input
Josahty 0:6942f0e2198c 136 // }
Josahty 0:6942f0e2198c 137 //float DumpTruck::setPivotSpeed(){
Josahty 0:6942f0e2198c 138 // pc.printf("Please enter the Pivot speed /n");
Josahty 0:6942f0e2198c 139 // float input = pc.getc();
Josahty 0:6942f0e2198c 140 // return input;
Josahty 0:6942f0e2198c 141 // }
Josahty 0:6942f0e2198c 142 //
Josahty 0:6942f0e2198c 143 //float DumpTruck::setBedSpeed(){
Josahty 0:6942f0e2198c 144 // pc.printf("Please enter the Bed speed /n");
Josahty 0:6942f0e2198c 145 // float input = pc.getc();
Josahty 0:6942f0e2198c 146 // return input;
Josahty 0:6942f0e2198c 147 // }
Josahty 0:6942f0e2198c 148 //float DumpTruck::getBedAngle(){
Josahty 0:6942f0e2198c 149 // float input = AnalogIn(x); // data from potentiometer
Josahty 0:6942f0e2198c 150 // return input;
Josahty 0:6942f0e2198c 151 // }
Josahty 0:6942f0e2198c 152 //
Josahty 0:6942f0e2198c 153