BETZtechnik openPnP feeder driver firmware

Dependencies:   mbed

Committer:
BETZtechnik
Date:
Fri Feb 03 05:36:03 2017 +0000
Revision:
0:eaf3f7d442bd
V1- for testing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
BETZtechnik 0:eaf3f7d442bd 1 #include "mbed.h"
BETZtechnik 0:eaf3f7d442bd 2
BETZtechnik 0:eaf3f7d442bd 3 // JUMPERS REQUIRED ON MOTOR DRIVER STBY PINS ! PULL TO 3V3.
BETZtechnik 0:eaf3f7d442bd 4 // Feeder message is FxxTx(L or R) F= feeder ID, T= teethRequested, (L for left or R for right)
BETZtechnik 0:eaf3f7d442bd 5 // *************************************************
BETZtechnik 0:eaf3f7d442bd 6 // FEEDER ID !! Set 10's and 1's (for ID= 13, tens = 1 and ones = 3) USE CHAR!
BETZtechnik 0:eaf3f7d442bd 7 char tens = '0';
BETZtechnik 0:eaf3f7d442bd 8 char ones = '1';
BETZtechnik 0:eaf3f7d442bd 9
BETZtechnik 0:eaf3f7d442bd 10
BETZtechnik 0:eaf3f7d442bd 11 int teethRequested = 0; // how many teeth advance has been selected?
BETZtechnik 0:eaf3f7d442bd 12 int index = 0; // for parsing serial
BETZtechnik 0:eaf3f7d442bd 13
BETZtechnik 0:eaf3f7d442bd 14
BETZtechnik 0:eaf3f7d442bd 15 Timer timer;
BETZtechnik 0:eaf3f7d442bd 16
BETZtechnik 0:eaf3f7d442bd 17 InterruptIn lOpto(P1_31);
BETZtechnik 0:eaf3f7d442bd 18 InterruptIn lButton(P1_25);
BETZtechnik 0:eaf3f7d442bd 19 InterruptIn rOpto(P1_29);
BETZtechnik 0:eaf3f7d442bd 20 InterruptIn rButton(P1_26);
BETZtechnik 0:eaf3f7d442bd 21
BETZtechnik 0:eaf3f7d442bd 22 //LEDs
BETZtechnik 0:eaf3f7d442bd 23 DigitalOut lLed(P0_18);
BETZtechnik 0:eaf3f7d442bd 24 DigitalOut rLed(P1_16);
BETZtechnik 0:eaf3f7d442bd 25
BETZtechnik 0:eaf3f7d442bd 26 //Left Tape
BETZtechnik 0:eaf3f7d442bd 27 DigitalOut lTapeIn1(P1_20);
BETZtechnik 0:eaf3f7d442bd 28 DigitalOut lTapeIn2(P1_21);
BETZtechnik 0:eaf3f7d442bd 29 PwmOut lTapePWM(P1_24);
BETZtechnik 0:eaf3f7d442bd 30 AnalogIn tapePot(P0_12); // will be 0 -1024
BETZtechnik 0:eaf3f7d442bd 31 int tapePotADC; // after math will be 0-9 for PWMwrite
BETZtechnik 0:eaf3f7d442bd 32
BETZtechnik 0:eaf3f7d442bd 33 //Left motor
BETZtechnik 0:eaf3f7d442bd 34 DigitalOut lMotorIn1(P0_15);
BETZtechnik 0:eaf3f7d442bd 35 DigitalOut lMotorIn2(P0_16);
BETZtechnik 0:eaf3f7d442bd 36 PwmOut lMotorPWM(P1_24);
BETZtechnik 0:eaf3f7d442bd 37 int lMotorSet = 5; // use 1-10 (whole numbers) to set PWM, it is /10 in the function to be float (lMotorSet / 10)
BETZtechnik 0:eaf3f7d442bd 38
BETZtechnik 0:eaf3f7d442bd 39 // Right Tape
BETZtechnik 0:eaf3f7d442bd 40 DigitalOut rTapeIn1(P0_22);
BETZtechnik 0:eaf3f7d442bd 41 DigitalOut rTapeIn2(P0_21);
BETZtechnik 0:eaf3f7d442bd 42 PwmOut rTapePWM(P1_24);
BETZtechnik 0:eaf3f7d442bd 43
BETZtechnik 0:eaf3f7d442bd 44 //Right motor
BETZtechnik 0:eaf3f7d442bd 45 DigitalOut rMotorIn1(P0_23);
BETZtechnik 0:eaf3f7d442bd 46 DigitalOut rMotorIn2(P1_15);
BETZtechnik 0:eaf3f7d442bd 47 PwmOut rMotorPWM(P1_24);
BETZtechnik 0:eaf3f7d442bd 48 int rMotorSet = 5; // use 1-10 (whole numbers) to set PWM, it is /10 in the function to be float (lMotorSet / 10)
BETZtechnik 0:eaf3f7d442bd 49
BETZtechnik 0:eaf3f7d442bd 50 //////////////////////////////////////////////////////////
BETZtechnik 0:eaf3f7d442bd 51
BETZtechnik 0:eaf3f7d442bd 52 void lFeed(){
BETZtechnik 0:eaf3f7d442bd 53 if (teethRequested == 0){ // this is just for the button, if over serial then teethrequested has already been set to > 0
BETZtechnik 0:eaf3f7d442bd 54 teethRequested = 1; // change to how many teeth you want the button to feed
BETZtechnik 0:eaf3f7d442bd 55 }
BETZtechnik 0:eaf3f7d442bd 56
BETZtechnik 0:eaf3f7d442bd 57 //Tape
BETZtechnik 0:eaf3f7d442bd 58 lTapeIn1 = 0; // reverse 1's and 0's if rotation is backwards
BETZtechnik 0:eaf3f7d442bd 59 lTapeIn2 = 1;
BETZtechnik 0:eaf3f7d442bd 60 lTapePWM.write((tapePotADC / 10)); // from POT , max 0.9 with this code as ADC value is being divided by 103
BETZtechnik 0:eaf3f7d442bd 61
BETZtechnik 0:eaf3f7d442bd 62 //Motor
BETZtechnik 0:eaf3f7d442bd 63 lMotorIn1 = 0;
BETZtechnik 0:eaf3f7d442bd 64 lMotorIn2 = 1;
BETZtechnik 0:eaf3f7d442bd 65 lMotorPWM.write(lMotorSet / 10);
BETZtechnik 0:eaf3f7d442bd 66 }
BETZtechnik 0:eaf3f7d442bd 67
BETZtechnik 0:eaf3f7d442bd 68 void rFeed(){
BETZtechnik 0:eaf3f7d442bd 69 if (teethRequested == 0){ // this is just for the button, if over serial then teethrequested has already been set to > 0
BETZtechnik 0:eaf3f7d442bd 70 teethRequested = 1; // change to how many teeth you want the button to feed
BETZtechnik 0:eaf3f7d442bd 71 }
BETZtechnik 0:eaf3f7d442bd 72
BETZtechnik 0:eaf3f7d442bd 73 //Tape
BETZtechnik 0:eaf3f7d442bd 74 rTapeIn1 = 0; // reverse 1's and 0's if rotation is backwards
BETZtechnik 0:eaf3f7d442bd 75 rTapeIn2 = 1;
BETZtechnik 0:eaf3f7d442bd 76 rTapePWM.write((tapePotADC / 10)); // from POT , max 0.9 with this code as ADC value is being divided by 103
BETZtechnik 0:eaf3f7d442bd 77
BETZtechnik 0:eaf3f7d442bd 78 //Motor
BETZtechnik 0:eaf3f7d442bd 79 rMotorIn1 = 0;
BETZtechnik 0:eaf3f7d442bd 80 rMotorIn2 = 1;
BETZtechnik 0:eaf3f7d442bd 81 rMotorPWM.write(lMotorSet / 10);
BETZtechnik 0:eaf3f7d442bd 82 }
BETZtechnik 0:eaf3f7d442bd 83
BETZtechnik 0:eaf3f7d442bd 84 void lStop(){
BETZtechnik 0:eaf3f7d442bd 85 if (teethRequested == 1){ // If only one (or one more) tooth needed to be driven
BETZtechnik 0:eaf3f7d442bd 86
BETZtechnik 0:eaf3f7d442bd 87 //tape
BETZtechnik 0:eaf3f7d442bd 88 lTapeIn1 = 0;
BETZtechnik 0:eaf3f7d442bd 89 lTapeIn2 = 0;
BETZtechnik 0:eaf3f7d442bd 90 lTapePWM.write(0.0f); //PWM off
BETZtechnik 0:eaf3f7d442bd 91
BETZtechnik 0:eaf3f7d442bd 92 //motor
BETZtechnik 0:eaf3f7d442bd 93 lMotorIn1 = 0;
BETZtechnik 0:eaf3f7d442bd 94 lMotorIn2 = 0;
BETZtechnik 0:eaf3f7d442bd 95 lMotorPWM.write(0.0f); //PWM off
BETZtechnik 0:eaf3f7d442bd 96 teethRequested = 0; // set teeth requested to zero so it will ignore further opto interrupts.
BETZtechnik 0:eaf3f7d442bd 97 }
BETZtechnik 0:eaf3f7d442bd 98
BETZtechnik 0:eaf3f7d442bd 99 if (teethRequested <=2){ // If more than one tooth needs to be driven, skip this interrupt and decrement
BETZtechnik 0:eaf3f7d442bd 100 teethRequested = (teethRequested - 1);
BETZtechnik 0:eaf3f7d442bd 101 }
BETZtechnik 0:eaf3f7d442bd 102 }
BETZtechnik 0:eaf3f7d442bd 103
BETZtechnik 0:eaf3f7d442bd 104 void rStop(){
BETZtechnik 0:eaf3f7d442bd 105 if (teethRequested == 1){ // If only one (or one more) tooth needed to be driven
BETZtechnik 0:eaf3f7d442bd 106
BETZtechnik 0:eaf3f7d442bd 107 //tape
BETZtechnik 0:eaf3f7d442bd 108 rTapeIn1 = 0;
BETZtechnik 0:eaf3f7d442bd 109 rTapeIn2 = 0;
BETZtechnik 0:eaf3f7d442bd 110 rTapePWM.write(0.0f); //PWM off
BETZtechnik 0:eaf3f7d442bd 111
BETZtechnik 0:eaf3f7d442bd 112 //motor
BETZtechnik 0:eaf3f7d442bd 113 rMotorIn1 = 0;
BETZtechnik 0:eaf3f7d442bd 114 rMotorIn2 = 0;
BETZtechnik 0:eaf3f7d442bd 115 rMotorPWM.write(0.0f); //PWM off
BETZtechnik 0:eaf3f7d442bd 116 teethRequested = 0; // set teeth requested to zero so it will ignore further opto interrupts.
BETZtechnik 0:eaf3f7d442bd 117 }
BETZtechnik 0:eaf3f7d442bd 118
BETZtechnik 0:eaf3f7d442bd 119 if (teethRequested <=2){ // If more than one tooth needs to be driven, skip this interrupt and decrement
BETZtechnik 0:eaf3f7d442bd 120 teethRequested = (teethRequested - 1);
BETZtechnik 0:eaf3f7d442bd 121 }
BETZtechnik 0:eaf3f7d442bd 122 }
BETZtechnik 0:eaf3f7d442bd 123
BETZtechnik 0:eaf3f7d442bd 124 // SERIAL
BETZtechnik 0:eaf3f7d442bd 125
BETZtechnik 0:eaf3f7d442bd 126 Serial device(P1_13, P1_14); // RS 485
BETZtechnik 0:eaf3f7d442bd 127
BETZtechnik 0:eaf3f7d442bd 128
BETZtechnik 0:eaf3f7d442bd 129 int main() {
BETZtechnik 0:eaf3f7d442bd 130
BETZtechnik 0:eaf3f7d442bd 131 lOpto.mode(PullNone); // external pull up
BETZtechnik 0:eaf3f7d442bd 132 lButton.mode(PullUp); // button pull up
BETZtechnik 0:eaf3f7d442bd 133 rOpto.mode(PullNone);
BETZtechnik 0:eaf3f7d442bd 134
BETZtechnik 0:eaf3f7d442bd 135 lOpto.fall(&lStop);
BETZtechnik 0:eaf3f7d442bd 136 lButton.fall(&lFeed);
BETZtechnik 0:eaf3f7d442bd 137
BETZtechnik 0:eaf3f7d442bd 138 uint8_t c = 0;
BETZtechnik 0:eaf3f7d442bd 139 device.printf("Hello World\n");
BETZtechnik 0:eaf3f7d442bd 140
BETZtechnik 0:eaf3f7d442bd 141 while(1) {
BETZtechnik 0:eaf3f7d442bd 142
BETZtechnik 0:eaf3f7d442bd 143 // TODO create timer for USB debug messages
BETZtechnik 0:eaf3f7d442bd 144
BETZtechnik 0:eaf3f7d442bd 145 while (device.readable())
BETZtechnik 0:eaf3f7d442bd 146 {
BETZtechnik 0:eaf3f7d442bd 147 c = device.getc();
BETZtechnik 0:eaf3f7d442bd 148 if (c == 'F'){
BETZtechnik 0:eaf3f7d442bd 149 index = 1;
BETZtechnik 0:eaf3f7d442bd 150 }
BETZtechnik 0:eaf3f7d442bd 151
BETZtechnik 0:eaf3f7d442bd 152 if (index == 1){
BETZtechnik 0:eaf3f7d442bd 153 if (c == tens){
BETZtechnik 0:eaf3f7d442bd 154 index = 2;
BETZtechnik 0:eaf3f7d442bd 155 }
BETZtechnik 0:eaf3f7d442bd 156 }
BETZtechnik 0:eaf3f7d442bd 157
BETZtechnik 0:eaf3f7d442bd 158 if (index == 2){
BETZtechnik 0:eaf3f7d442bd 159 if (c == ones);{
BETZtechnik 0:eaf3f7d442bd 160 index = 3;
BETZtechnik 0:eaf3f7d442bd 161 }
BETZtechnik 0:eaf3f7d442bd 162 }
BETZtechnik 0:eaf3f7d442bd 163
BETZtechnik 0:eaf3f7d442bd 164 if (index == 3){
BETZtechnik 0:eaf3f7d442bd 165 if (c == '1'){
BETZtechnik 0:eaf3f7d442bd 166 teethRequested = 1;
BETZtechnik 0:eaf3f7d442bd 167 }
BETZtechnik 0:eaf3f7d442bd 168 else if (c == '2'){
BETZtechnik 0:eaf3f7d442bd 169 teethRequested = 2;
BETZtechnik 0:eaf3f7d442bd 170 }
BETZtechnik 0:eaf3f7d442bd 171 else if (c == '3'){
BETZtechnik 0:eaf3f7d442bd 172 teethRequested = 3;
BETZtechnik 0:eaf3f7d442bd 173 }
BETZtechnik 0:eaf3f7d442bd 174 else if (c == '4'){
BETZtechnik 0:eaf3f7d442bd 175 teethRequested = 4;
BETZtechnik 0:eaf3f7d442bd 176 }
BETZtechnik 0:eaf3f7d442bd 177 index = 4;
BETZtechnik 0:eaf3f7d442bd 178 }
BETZtechnik 0:eaf3f7d442bd 179
BETZtechnik 0:eaf3f7d442bd 180 if (index == 4){
BETZtechnik 0:eaf3f7d442bd 181 if (c == 'L'){
BETZtechnik 0:eaf3f7d442bd 182 lFeed();
BETZtechnik 0:eaf3f7d442bd 183 index = 0;
BETZtechnik 0:eaf3f7d442bd 184 }
BETZtechnik 0:eaf3f7d442bd 185 }
BETZtechnik 0:eaf3f7d442bd 186 if (c == 'R'){
BETZtechnik 0:eaf3f7d442bd 187 rFeed();
BETZtechnik 0:eaf3f7d442bd 188 index = 0;
BETZtechnik 0:eaf3f7d442bd 189 }
BETZtechnik 0:eaf3f7d442bd 190 }
BETZtechnik 0:eaf3f7d442bd 191
BETZtechnik 0:eaf3f7d442bd 192
BETZtechnik 0:eaf3f7d442bd 193 tapePotADC = ((tapePot)/ 103); // covert 1024 to (1 -9) for the PWM setting.
BETZtechnik 0:eaf3f7d442bd 194
BETZtechnik 0:eaf3f7d442bd 195
BETZtechnik 0:eaf3f7d442bd 196
BETZtechnik 0:eaf3f7d442bd 197 if(lOpto){
BETZtechnik 0:eaf3f7d442bd 198 lLed = 0;}
BETZtechnik 0:eaf3f7d442bd 199 else{
BETZtechnik 0:eaf3f7d442bd 200 lLed = 1;
BETZtechnik 0:eaf3f7d442bd 201 }
BETZtechnik 0:eaf3f7d442bd 202 }
BETZtechnik 0:eaf3f7d442bd 203
BETZtechnik 0:eaf3f7d442bd 204 }