![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
update hcsr04.cpp by changing 20000 to 2000
Dependencies: Stepper mbed SDFileSystem Ultrasonic PinDetect millis
main.cpp@2:e8f8095464cf, 2018-09-14 (annotated)
- Committer:
- rschimpf78
- Date:
- Fri Sep 14 19:10:45 2018 +0000
- Revision:
- 2:e8f8095464cf
- Parent:
- 0:88834eed3de0
- Child:
- 3:fd2ca631ab44
code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rschimpf78 | 2:e8f8095464cf | 1 | #include "mbed.h" |
rschimpf78 | 2:e8f8095464cf | 2 | #include "Stepper.h" |
rschimpf78 | 2:e8f8095464cf | 3 | #include "string" |
rschimpf78 | 2:e8f8095464cf | 4 | DigitalOut in1(D10);///in1 d9 and in2 d10 |
rschimpf78 | 2:e8f8095464cf | 5 | DigitalOut in2(D9); |
rschimpf78 | 2:e8f8095464cf | 6 | DigitalIn endSwitch(D6); |
rschimpf78 | 2:e8f8095464cf | 7 | DigitalIn homeSwitch(D7); |
rschimpf78 | 2:e8f8095464cf | 8 | DigitalIn bottomSwitch(D5); |
rschimpf78 | 2:e8f8095464cf | 9 | Stepper mot(D3,D4); //(D5 to PUL+, D4 to DIR+) |
rschimpf78 | 2:e8f8095464cf | 10 | DigitalOut en(D2); |
rschimpf78 | 0:88834eed3de0 | 11 | |
rschimpf78 | 0:88834eed3de0 | 12 | Serial pc(USBTX, USBRX); |
rschimpf78 | 0:88834eed3de0 | 13 | |
rschimpf78 | 2:e8f8095464cf | 14 | volatile int plates = 1; |
rschimpf78 | 2:e8f8095464cf | 15 | |
rschimpf78 | 2:e8f8095464cf | 16 | void position() |
rschimpf78 | 0:88834eed3de0 | 17 | { |
rschimpf78 | 2:e8f8095464cf | 18 | char str1[20]; |
rschimpf78 | 2:e8f8095464cf | 19 | char str2[20]; |
rschimpf78 | 2:e8f8095464cf | 20 | char str3[20]; |
rschimpf78 | 2:e8f8095464cf | 21 | strcpy(str1,"HOME"); |
rschimpf78 | 2:e8f8095464cf | 22 | strcpy(str2,"ERROR"); |
rschimpf78 | 2:e8f8095464cf | 23 | strcpy(str3,"IN BEAM PATH"); |
rschimpf78 | 2:e8f8095464cf | 24 | |
rschimpf78 | 2:e8f8095464cf | 25 | if(homeSwitch==0 && endSwitch==1) |
rschimpf78 | 2:e8f8095464cf | 26 | { |
rschimpf78 | 2:e8f8095464cf | 27 | pc.printf(" pmma state - %s\n",str1); |
rschimpf78 | 2:e8f8095464cf | 28 | } |
rschimpf78 | 2:e8f8095464cf | 29 | else if (homeSwitch==1 && endSwitch==0) |
rschimpf78 | 2:e8f8095464cf | 30 | { |
rschimpf78 | 2:e8f8095464cf | 31 | pc.printf(" pmma state - %s\n",str3); |
rschimpf78 | 2:e8f8095464cf | 32 | } |
rschimpf78 | 2:e8f8095464cf | 33 | else if (homeSwitch==1 && endSwitch==1) |
rschimpf78 | 2:e8f8095464cf | 34 | { |
rschimpf78 | 2:e8f8095464cf | 35 | pc.printf(" pmma state - %s\n",str2); |
rschimpf78 | 2:e8f8095464cf | 36 | } |
rschimpf78 | 2:e8f8095464cf | 37 | else |
rschimpf78 | 2:e8f8095464cf | 38 | { |
rschimpf78 | 2:e8f8095464cf | 39 | pc.printf(" pmma state - else"); |
rschimpf78 | 2:e8f8095464cf | 40 | } |
rschimpf78 | 2:e8f8095464cf | 41 | } |
rschimpf78 | 0:88834eed3de0 | 42 | void retractPMMA() |
rschimpf78 | 0:88834eed3de0 | 43 | { |
rschimpf78 | 2:e8f8095464cf | 44 | while(homeSwitch == 1) |
rschimpf78 | 2:e8f8095464cf | 45 | { |
rschimpf78 | 0:88834eed3de0 | 46 | in1=1; |
rschimpf78 | 0:88834eed3de0 | 47 | in2=0; |
rschimpf78 | 2:e8f8095464cf | 48 | } |
rschimpf78 | 2:e8f8095464cf | 49 | in1=1; |
rschimpf78 | 2:e8f8095464cf | 50 | in2=1; |
rschimpf78 | 2:e8f8095464cf | 51 | } |
rschimpf78 | 2:e8f8095464cf | 52 | void sendPMMA() //Command: Send plates |
rschimpf78 | 2:e8f8095464cf | 53 | { |
rschimpf78 | 2:e8f8095464cf | 54 | |
rschimpf78 | 0:88834eed3de0 | 55 | in1=0; |
rschimpf78 | 2:e8f8095464cf | 56 | in2=1; |
rschimpf78 | 2:e8f8095464cf | 57 | wait(9); |
rschimpf78 | 2:e8f8095464cf | 58 | in1=1; |
rschimpf78 | 2:e8f8095464cf | 59 | in2=1; |
rschimpf78 | 0:88834eed3de0 | 60 | } |
rschimpf78 | 0:88834eed3de0 | 61 | |
rschimpf78 | 2:e8f8095464cf | 62 | void autoCalibrate() //if machine is plugged in when the plate selector isn't at the origin... |
rschimpf78 | 2:e8f8095464cf | 63 | { |
rschimpf78 | 2:e8f8095464cf | 64 | while(homeSwitch==1) |
rschimpf78 | 2:e8f8095464cf | 65 | { |
rschimpf78 | 2:e8f8095464cf | 66 | retractPMMA(); |
rschimpf78 | 2:e8f8095464cf | 67 | } |
rschimpf78 | 2:e8f8095464cf | 68 | |
rschimpf78 | 0:88834eed3de0 | 69 | in1=1; |
rschimpf78 | 2:e8f8095464cf | 70 | in2=1; |
rschimpf78 | 2:e8f8095464cf | 71 | en = 0; //...it automatically lowers it to the origin position |
rschimpf78 | 2:e8f8095464cf | 72 | mot.setSpeed(600); |
rschimpf78 | 2:e8f8095464cf | 73 | mot.rotate(1); |
rschimpf78 | 2:e8f8095464cf | 74 | while(bottomSwitch); //While bottom switch is unpressed |
rschimpf78 | 2:e8f8095464cf | 75 | mot.stop(); //Stop rotation when switch is pressed (pressed = logical level low) //Disable driver |
rschimpf78 | 2:e8f8095464cf | 76 | mot.setPositionZero(); |
rschimpf78 | 2:e8f8095464cf | 77 | wait(1); |
rschimpf78 | 2:e8f8095464cf | 78 | mot.goesTo(-800); |
rschimpf78 | 2:e8f8095464cf | 79 | while(!mot.stopped()); |
rschimpf78 | 2:e8f8095464cf | 80 | mot.stop(); |
rschimpf78 | 2:e8f8095464cf | 81 | mot.setPositionZero(); |
rschimpf78 | 2:e8f8095464cf | 82 | en=1; //Set absolute origin for plate selector at switch hit |
rschimpf78 | 0:88834eed3de0 | 83 | } |
rschimpf78 | 0:88834eed3de0 | 84 | void internalpullups() |
rschimpf78 | 0:88834eed3de0 | 85 | { |
rschimpf78 | 2:e8f8095464cf | 86 | bottomSwitch.mode(PullUp); |
rschimpf78 | 2:e8f8095464cf | 87 | homeSwitch.mode(PullUp); |
rschimpf78 | 2:e8f8095464cf | 88 | endSwitch.mode(PullUp); |
rschimpf78 | 0:88834eed3de0 | 89 | wait (.01); |
rschimpf78 | 0:88834eed3de0 | 90 | } |
rschimpf78 | 2:e8f8095464cf | 91 | void numberofPlates() // Command: Number of PMMA to send (plates) |
rschimpf78 | 2:e8f8095464cf | 92 | { |
rschimpf78 | 2:e8f8095464cf | 93 | int stepheight; |
rschimpf78 | 2:e8f8095464cf | 94 | |
rschimpf78 | 2:e8f8095464cf | 95 | if (plates <= 0 || plates >= 17) |
rschimpf78 | 2:e8f8095464cf | 96 | { |
rschimpf78 | 2:e8f8095464cf | 97 | pc.printf(" ERROR invalid range |"); |
rschimpf78 | 2:e8f8095464cf | 98 | } |
rschimpf78 | 2:e8f8095464cf | 99 | else if(endSwitch == 1 && homeSwitch==0) |
rschimpf78 | 2:e8f8095464cf | 100 | { |
rschimpf78 | 2:e8f8095464cf | 101 | en = 0; |
rschimpf78 | 2:e8f8095464cf | 102 | mot.goesTo((plates-1)*-1680); //plates per step |
rschimpf78 | 2:e8f8095464cf | 103 | while(!mot.stopped()); |
rschimpf78 | 2:e8f8095464cf | 104 | stepheight = mot.getPosition(); |
rschimpf78 | 2:e8f8095464cf | 105 | pc.printf(" selector height %d steps |",stepheight); |
rschimpf78 | 2:e8f8095464cf | 106 | mot.stop(); |
rschimpf78 | 2:e8f8095464cf | 107 | en = 1; |
rschimpf78 | 2:e8f8095464cf | 108 | wait(1); |
rschimpf78 | 2:e8f8095464cf | 109 | sendPMMA(); |
rschimpf78 | 0:88834eed3de0 | 110 | } |
rschimpf78 | 2:e8f8095464cf | 111 | else |
rschimpf78 | 2:e8f8095464cf | 112 | { |
rschimpf78 | 2:e8f8095464cf | 113 | pc.printf("Position Error - Cannot Send"); |
rschimpf78 | 2:e8f8095464cf | 114 | } |
rschimpf78 | 2:e8f8095464cf | 115 | } |
rschimpf78 | 0:88834eed3de0 | 116 | void selectionStatus() |
rschimpf78 | 0:88834eed3de0 | 117 | { |
rschimpf78 | 2:e8f8095464cf | 118 | int stepperposition; |
rschimpf78 | 2:e8f8095464cf | 119 | double selectedplates; |
rschimpf78 | 2:e8f8095464cf | 120 | stepperposition = mot.getPosition(); |
rschimpf78 | 2:e8f8095464cf | 121 | selectedplates = stepperposition/1621; |
rschimpf78 | 2:e8f8095464cf | 122 | pc.printf("\n Number of plates currently selected: %d ",selectedplates); |
rschimpf78 | 2:e8f8095464cf | 123 | } |
rschimpf78 | 2:e8f8095464cf | 124 | void deviceinfo() |
rschimpf78 | 2:e8f8095464cf | 125 | { |
rschimpf78 | 2:e8f8095464cf | 126 | printf("\nUnique ID: 0240000041114e4500513007bcf9000a9e51000097969900"); |
rschimpf78 | 2:e8f8095464cf | 127 | printf("\nBootloader Version: 0244"); |
rschimpf78 | 2:e8f8095464cf | 128 | printf("\nBUILD: 1:4BDA04E+"); |
rschimpf78 | 2:e8f8095464cf | 129 | printf("\nBUILD TIMESTAMP=3/18/2018\n\n"); |
rschimpf78 | 2:e8f8095464cf | 130 | |
rschimpf78 | 2:e8f8095464cf | 131 | } |
rschimpf78 | 0:88834eed3de0 | 132 | |
rschimpf78 | 0:88834eed3de0 | 133 | int main() |
rschimpf78 | 2:e8f8095464cf | 134 | { |
rschimpf78 | 2:e8f8095464cf | 135 | in1=1; |
rschimpf78 | 2:e8f8095464cf | 136 | in2=1; |
rschimpf78 | 2:e8f8095464cf | 137 | en = 1; |
rschimpf78 | 2:e8f8095464cf | 138 | wait(0.1); |
rschimpf78 | 0:88834eed3de0 | 139 | internalpullups(); |
rschimpf78 | 2:e8f8095464cf | 140 | wait(0.1); |
rschimpf78 | 2:e8f8095464cf | 141 | autoCalibrate(); |
rschimpf78 | 2:e8f8095464cf | 142 | |
rschimpf78 | 2:e8f8095464cf | 143 | char command[15] = {0}; |
rschimpf78 | 2:e8f8095464cf | 144 | pc.printf("\nCommands:\n\n\tsend [N]\t**places N PMMA sheets in beam path\n\tretract\t\t**removes all PMMA from beam path\n\tlocation\t**returns location of PMMA\n\tinfo\t\t**returns device and firmware information\n\n"); |
rschimpf78 | 2:e8f8095464cf | 145 | |
rschimpf78 | 2:e8f8095464cf | 146 | while(1) |
rschimpf78 | 0:88834eed3de0 | 147 | { |
rschimpf78 | 2:e8f8095464cf | 148 | |
rschimpf78 | 2:e8f8095464cf | 149 | pc.printf("\nRequests: "); |
rschimpf78 | 2:e8f8095464cf | 150 | pc.scanf("%s",&command); |
rschimpf78 | 2:e8f8095464cf | 151 | |
rschimpf78 | 2:e8f8095464cf | 152 | if (strcmp (command, "send") == 0) |
rschimpf78 | 2:e8f8095464cf | 153 | { |
rschimpf78 | 2:e8f8095464cf | 154 | pc.scanf(" %d",&plates); |
rschimpf78 | 2:e8f8095464cf | 155 | pc.printf("\nresponse recieved |"); |
rschimpf78 | 2:e8f8095464cf | 156 | numberofPlates(); |
rschimpf78 | 2:e8f8095464cf | 157 | position(); |
rschimpf78 | 0:88834eed3de0 | 158 | } |
rschimpf78 | 2:e8f8095464cf | 159 | else if (strcmp (command, "retract") == 0) |
rschimpf78 | 2:e8f8095464cf | 160 | { |
rschimpf78 | 2:e8f8095464cf | 161 | pc.printf("\nresponse recieved |"); |
rschimpf78 | 2:e8f8095464cf | 162 | retractPMMA(); |
rschimpf78 | 2:e8f8095464cf | 163 | position(); |
rschimpf78 | 0:88834eed3de0 | 164 | } |
rschimpf78 | 2:e8f8095464cf | 165 | else if (strcmp (command, "location") == 0) |
rschimpf78 | 0:88834eed3de0 | 166 | { |
rschimpf78 | 2:e8f8095464cf | 167 | pc.printf("\nresponse recieved |"); |
rschimpf78 | 2:e8f8095464cf | 168 | position(); |
rschimpf78 | 2:e8f8095464cf | 169 | } |
rschimpf78 | 2:e8f8095464cf | 170 | else if (strcmp (command, "info") == 0) |
rschimpf78 | 2:e8f8095464cf | 171 | { |
rschimpf78 | 2:e8f8095464cf | 172 | pc.printf("\nresponse recieved |"); |
rschimpf78 | 2:e8f8095464cf | 173 | deviceinfo(); |
rschimpf78 | 0:88834eed3de0 | 174 | } |
rschimpf78 | 2:e8f8095464cf | 175 | else if (strcmp (command, "height") == 0) |
rschimpf78 | 2:e8f8095464cf | 176 | { |
rschimpf78 | 2:e8f8095464cf | 177 | pc.printf("\nresponse recieved |"); |
rschimpf78 | 2:e8f8095464cf | 178 | numberofPlates(); |
rschimpf78 | 0:88834eed3de0 | 179 | } |
rschimpf78 | 2:e8f8095464cf | 180 | |
rschimpf78 | 2:e8f8095464cf | 181 | else |
rschimpf78 | 2:e8f8095464cf | 182 | { |
rschimpf78 | 2:e8f8095464cf | 183 | pc.printf("\nERROR-command invalid"); |
rschimpf78 | 2:e8f8095464cf | 184 | } |
rschimpf78 | 0:88834eed3de0 | 185 | } |
rschimpf78 | 2:e8f8095464cf | 186 | } |