RTES / Mbed 2 deprecated mbed_pwmLib

Dependencies:   mbed

Committer:
prithviganeshk
Date:
Wed Oct 14 22:30:39 2015 +0000
Revision:
6:625384a34dd5
Parent:
5:7b4575bf205e
Child:
7:3bed3e005200
working with servos

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jiteshg 1:8c73948a0864 1 /*
jiteshg 1:8c73948a0864 2 Using pwm to run a servo motor
jiteshg 1:8c73948a0864 3 Connect the red wire of the servo motor to 3.3V and not 5V
jiteshg 2:bf817b299c19 4
jiteshg 2:bf817b299c19 5 DC motor pins - p5 and p6 to control the direction of the motor
jiteshg 2:bf817b299c19 6 p23 to control the duty cycle and period
jiteshg 1:8c73948a0864 7 */
jiteshg 0:fd080fb55bae 8 #include "mbed.h"
jiteshg 2:bf817b299c19 9 PwmOut pwm1(p21); //Servo Motor-1 PWM channel
jiteshg 2:bf817b299c19 10 PwmOut pwm2(p22); //Servo Motor-2 PWM channel
jiteshg 2:bf817b299c19 11
jiteshg 2:bf817b299c19 12 DigitalOut dc1(p5); //DC motor input-1
jiteshg 2:bf817b299c19 13 DigitalOut dc2(p6); //DC motor input-2
jiteshg 2:bf817b299c19 14 PwmOut pwm3(p23); //DC motor PWM channel
jiteshg 2:bf817b299c19 15
jiteshg 3:66a52943c525 16 AnalogIn button(p15); //Analog input from the floor buttons
jiteshg 4:acb62dee5ba9 17 InterruptIn event(p7); //Interrupt for rising and falling edge detection from IR
jiteshg 3:66a52943c525 18
jiteshg 4:acb62dee5ba9 19 Timer timer; //Timer to read falling and rising edge time
prithviganeshk 6:625384a34dd5 20 Timer timer2;
jiteshg 4:acb62dee5ba9 21 Serial pc(USBTX, USBRX);//Serial Communication
jiteshg 0:fd080fb55bae 22
jiteshg 2:bf817b299c19 23 void openGate();
jiteshg 2:bf817b299c19 24 void closeGate();
jiteshg 4:acb62dee5ba9 25 void rising();
jiteshg 4:acb62dee5ba9 26 void falling();
prithviganeshk 6:625384a34dd5 27 void start_pwm2(int x);
jiteshg 4:acb62dee5ba9 28
jiteshg 3:66a52943c525 29 int getState(float adc_val);
jiteshg 3:66a52943c525 30
jiteshg 2:bf817b299c19 31 int currentState = 1;
jiteshg 4:acb62dee5ba9 32 int begin,end = 0;
jiteshg 4:acb62dee5ba9 33 float frequency = 0;
jiteshg 4:acb62dee5ba9 34 bool flag = false;
jiteshg 5:7b4575bf205e 35 //int fvalues[] = {0,100,250,500,700,1000};
jiteshg 5:7b4575bf205e 36 int fvalues[] = {0,102,270,615,948,1623};
jiteshg 2:bf817b299c19 37
jiteshg 0:fd080fb55bae 38 int main() {
jiteshg 4:acb62dee5ba9 39 timer.start();
jiteshg 4:acb62dee5ba9 40 event.rise(&rising);
jiteshg 4:acb62dee5ba9 41 event.fall(&falling);
jiteshg 2:bf817b299c19 42 //Setting dc1 to high and dc2 to low initially
jiteshg 4:acb62dee5ba9 43 dc1 = 0;
jiteshg 4:acb62dee5ba9 44 dc2 = 1;
jiteshg 2:bf817b299c19 45 pwm3.period_ms(20);
jiteshg 2:bf817b299c19 46 pwm3.write(0);
jiteshg 2:bf817b299c19 47
jiteshg 2:bf817b299c19 48 //Setting the period and duty cycle for Servo motors
jiteshg 0:fd080fb55bae 49 pwm1.period_ms(20);
jiteshg 2:bf817b299c19 50 pwm2.period_ms(20);
jiteshg 2:bf817b299c19 51 pwm1.write(0);
jiteshg 2:bf817b299c19 52 pwm2.write(0);
jiteshg 1:8c73948a0864 53
jiteshg 1:8c73948a0864 54 while(1){
prithviganeshk 6:625384a34dd5 55 //printf("Frequency:-%f\n", frequency);
prithviganeshk 6:625384a34dd5 56 char c = pc.getc();
prithviganeshk 6:625384a34dd5 57 int val = c - 48;
prithviganeshk 6:625384a34dd5 58 //float adc_val = button.read();
prithviganeshk 6:625384a34dd5 59 //int val = getState(adc_val);
jiteshg 4:acb62dee5ba9 60 pc.printf("Floor-%d\n",val);
jiteshg 4:acb62dee5ba9 61 pc.printf("CurrentState-%d\n",currentState);
jiteshg 4:acb62dee5ba9 62 wait(1);
jiteshg 2:bf817b299c19 63 if(val==currentState){
jiteshg 2:bf817b299c19 64 pwm3.write(0);
jiteshg 2:bf817b299c19 65 }
jiteshg 2:bf817b299c19 66 else if(val > currentState){
jiteshg 4:acb62dee5ba9 67 closeGate(); //Close gate
jiteshg 2:bf817b299c19 68 //Move Up
jiteshg 4:acb62dee5ba9 69 dc1 = 0;
jiteshg 4:acb62dee5ba9 70 dc2 = 1;
prithviganeshk 6:625384a34dd5 71 pwm3.write(0.1);
jiteshg 4:acb62dee5ba9 72 //wait(2);
jiteshg 4:acb62dee5ba9 73 pc.printf("Floor Frequency value:%d\n",fvalues[val]);
jiteshg 4:acb62dee5ba9 74 while(1){
jiteshg 5:7b4575bf205e 75 if(((frequency > (fvalues[val] - 2)) && (frequency < (fvalues[val] + 2)))){
jiteshg 4:acb62dee5ba9 76 break;
jiteshg 4:acb62dee5ba9 77 }
prithviganeshk 6:625384a34dd5 78 //else{
prithviganeshk 6:625384a34dd5 79 //printf("current freq: %f\n",frequency);
prithviganeshk 6:625384a34dd5 80 //}
jiteshg 4:acb62dee5ba9 81 }
jiteshg 4:acb62dee5ba9 82 //while(!((frequency > (fvalues[val] - 50)) && (frequency < (fvalues[val] + 50))));
jiteshg 4:acb62dee5ba9 83 pwm3.write(0);
jiteshg 4:acb62dee5ba9 84 openGate();
jiteshg 4:acb62dee5ba9 85 }else{
jiteshg 4:acb62dee5ba9 86 closeGate(); //Close gate
jiteshg 4:acb62dee5ba9 87 //Move Down
jiteshg 2:bf817b299c19 88 dc1 = 1;
jiteshg 2:bf817b299c19 89 dc2 = 0;
prithviganeshk 6:625384a34dd5 90 pwm3.write(0.1);
jiteshg 4:acb62dee5ba9 91 //wait(2);
jiteshg 4:acb62dee5ba9 92 pc.printf("Floor Frequency value:-%d\n",fvalues[val]);
jiteshg 4:acb62dee5ba9 93 //while(!((frequency > (fvalues[val] - 50)) && (frequency < (fvalues[val] + 50))));
jiteshg 4:acb62dee5ba9 94 while(1){
jiteshg 5:7b4575bf205e 95 if(((frequency > (fvalues[val] - 2)) && (frequency < (fvalues[val] + 2)))){
jiteshg 4:acb62dee5ba9 96 break;
jiteshg 4:acb62dee5ba9 97 }
prithviganeshk 6:625384a34dd5 98 //else{
prithviganeshk 6:625384a34dd5 99 // printf("current freq: %f\n",frequency);
prithviganeshk 6:625384a34dd5 100 //}
jiteshg 4:acb62dee5ba9 101 }
jiteshg 2:bf817b299c19 102 pwm3.write(0);
jiteshg 4:acb62dee5ba9 103 openGate();
jiteshg 1:8c73948a0864 104 }
jiteshg 2:bf817b299c19 105 currentState = val;
jiteshg 1:8c73948a0864 106 }
jiteshg 2:bf817b299c19 107 }
jiteshg 2:bf817b299c19 108
jiteshg 2:bf817b299c19 109 void openGate(){
jiteshg 2:bf817b299c19 110 pwm1.write(0.0375); // 3.75% duty cycle - Open the gate
prithviganeshk 6:625384a34dd5 111 pwm2.write(0.1125);
prithviganeshk 6:625384a34dd5 112 //start_pwm2(1); // 11.25% duty cycle - Open the gate
prithviganeshk 6:625384a34dd5 113 wait(2);
jiteshg 2:bf817b299c19 114 pwm1.write(0); // Stop
prithviganeshk 6:625384a34dd5 115 pwm2.write(0);
jiteshg 0:fd080fb55bae 116 }
jiteshg 2:bf817b299c19 117
jiteshg 2:bf817b299c19 118 void closeGate(){
jiteshg 2:bf817b299c19 119 pwm1.write(0.1125); // 11.25% duty cycle - Close the gate
prithviganeshk 6:625384a34dd5 120 pwm2.write(0.0375);
prithviganeshk 6:625384a34dd5 121 //start_pwm2(0); // 3.75% duty cycle - Close the gate
prithviganeshk 6:625384a34dd5 122 wait(2);
jiteshg 2:bf817b299c19 123 pwm1.write(0); // Stop
prithviganeshk 6:625384a34dd5 124 pwm2.write(0);
prithviganeshk 6:625384a34dd5 125 //start_pwm2(0); // 3.75% duty cycle - Close the gate
jiteshg 3:66a52943c525 126 }
jiteshg 3:66a52943c525 127
jiteshg 3:66a52943c525 128 int getState(float adc_val){
jiteshg 3:66a52943c525 129 int state = 0;
jiteshg 3:66a52943c525 130 if(adc_val > 0.15 && adc_val < 0.25){
jiteshg 3:66a52943c525 131 state = 1;
jiteshg 3:66a52943c525 132 }
jiteshg 3:66a52943c525 133 else if(adc_val > 0.35 && adc_val < 0.45){
jiteshg 3:66a52943c525 134 state = 2;
jiteshg 3:66a52943c525 135 }
jiteshg 3:66a52943c525 136 else if(adc_val > 0.55 && adc_val < 0.65){
jiteshg 3:66a52943c525 137 state = 3;
jiteshg 3:66a52943c525 138 }
jiteshg 3:66a52943c525 139 else if(adc_val > 0.75 && adc_val < 0.85){
jiteshg 3:66a52943c525 140 state = 4;
jiteshg 3:66a52943c525 141 }
jiteshg 3:66a52943c525 142 else if(adc_val > 0.95 && adc_val < 1.05){
jiteshg 3:66a52943c525 143 state = 5;
jiteshg 3:66a52943c525 144 }
jiteshg 3:66a52943c525 145 return state;
jiteshg 4:acb62dee5ba9 146 }
jiteshg 4:acb62dee5ba9 147
jiteshg 4:acb62dee5ba9 148 void rising(){
jiteshg 4:acb62dee5ba9 149 begin = timer.read_us();
jiteshg 4:acb62dee5ba9 150 flag = true;
jiteshg 4:acb62dee5ba9 151 }
jiteshg 4:acb62dee5ba9 152 void falling(){
jiteshg 4:acb62dee5ba9 153 if(flag == true){
jiteshg 4:acb62dee5ba9 154 end = timer.read_us();
jiteshg 4:acb62dee5ba9 155 frequency = 500000/(end-begin);
jiteshg 4:acb62dee5ba9 156 begin = 0;
jiteshg 4:acb62dee5ba9 157 end = 0;
jiteshg 4:acb62dee5ba9 158 flag = false;
jiteshg 4:acb62dee5ba9 159 }
prithviganeshk 6:625384a34dd5 160 }
prithviganeshk 6:625384a34dd5 161
prithviganeshk 6:625384a34dd5 162 void start_pwm2(int x){
prithviganeshk 6:625384a34dd5 163
prithviganeshk 6:625384a34dd5 164 int duty1, duty2 = 0;
prithviganeshk 6:625384a34dd5 165
prithviganeshk 6:625384a34dd5 166 //printf("Reached startpwm2\ 1 n");
prithviganeshk 6:625384a34dd5 167 if(x == 1){
prithviganeshk 6:625384a34dd5 168 duty1 = 250; // .25ms on period in us
prithviganeshk 6:625384a34dd5 169 duty2 = 19750; // 19.75ms off period
prithviganeshk 6:625384a34dd5 170 }
prithviganeshk 6:625384a34dd5 171 else if(x == 0){
prithviganeshk 6:625384a34dd5 172 //printf("Reached startpwm2 2 \n");
prithviganeshk 6:625384a34dd5 173 duty1 = 2250; // 2.25ms on period
prithviganeshk 6:625384a34dd5 174 duty2 = 17750; //17.75ms off period
prithviganeshk 6:625384a34dd5 175 }
prithviganeshk 6:625384a34dd5 176
prithviganeshk 6:625384a34dd5 177 timer2.start();
prithviganeshk 6:625384a34dd5 178 int start1 = timer2.read_ms();
prithviganeshk 6:625384a34dd5 179 printf("%d\n", start1);
prithviganeshk 6:625384a34dd5 180
prithviganeshk 6:625384a34dd5 181 while((timer2.read_ms() - start1) < 2000){
prithviganeshk 6:625384a34dd5 182 //printf("entered");
prithviganeshk 6:625384a34dd5 183 pwm2 = 1;
prithviganeshk 6:625384a34dd5 184 wait_us(duty1);
prithviganeshk 6:625384a34dd5 185 pwm2 = 0;
prithviganeshk 6:625384a34dd5 186 wait_us(duty2);
prithviganeshk 6:625384a34dd5 187 }
prithviganeshk 6:625384a34dd5 188
prithviganeshk 6:625384a34dd5 189 pwm2=0;
prithviganeshk 6:625384a34dd5 190
prithviganeshk 6:625384a34dd5 191
prithviganeshk 6:625384a34dd5 192 }