moving plus angle

Dependencies:   QEI mbed

Committer:
LanierUSNA16
Date:
Thu Mar 26 12:30:15 2015 +0000
Revision:
1:d4c2e72540db
Parent:
0:8d7b6e55609a
Updated Gain Control;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
LanierUSNA16 0:8d7b6e55609a 1 #include "mbed.h"
LanierUSNA16 0:8d7b6e55609a 2 #include "QEI.h"
LanierUSNA16 0:8d7b6e55609a 3
LanierUSNA16 0:8d7b6e55609a 4 PwmOut turret_speed(p21);
LanierUSNA16 0:8d7b6e55609a 5 DigitalOut turret_direction(p22);
LanierUSNA16 0:8d7b6e55609a 6
LanierUSNA16 0:8d7b6e55609a 7 QEI myEncoder (p16,p15, NC, 1600);
LanierUSNA16 0:8d7b6e55609a 8 Timer t;
LanierUSNA16 0:8d7b6e55609a 9
LanierUSNA16 0:8d7b6e55609a 10 int main()
LanierUSNA16 0:8d7b6e55609a 11 {
LanierUSNA16 0:8d7b6e55609a 12 int input_dc =50;
LanierUSNA16 0:8d7b6e55609a 13 int user_direction = 0;
LanierUSNA16 1:d4c2e72540db 14 float duty_cycle = 0.0;
LanierUSNA16 0:8d7b6e55609a 15 int pulses = 0;
LanierUSNA16 0:8d7b6e55609a 16 float theta = 0.0;
LanierUSNA16 0:8d7b6e55609a 17 float time =0.0;
LanierUSNA16 1:d4c2e72540db 18 float signed_duty_cycle = 0.0;
LanierUSNA16 1:d4c2e72540db 19 float Kp = 1;
LanierUSNA16 1:d4c2e72540db 20
LanierUSNA16 1:d4c2e72540db 21 float theta_ref = 3.14/4.0;
LanierUSNA16 0:8d7b6e55609a 22
LanierUSNA16 0:8d7b6e55609a 23 while(1)
LanierUSNA16 0:8d7b6e55609a 24 {
LanierUSNA16 1:d4c2e72540db 25 //printf("Enter duty cycle, 0 to 100:\n");
LanierUSNA16 1:d4c2e72540db 26 //scanf("%d", &input_dc);
LanierUSNA16 0:8d7b6e55609a 27
LanierUSNA16 1:d4c2e72540db 28 //signed_duty_cycle = Kp*(theta_ref - theta);
LanierUSNA16 1:d4c2e72540db 29 //duty_cycle = input_dc/100.0;
LanierUSNA16 1:d4c2e72540db 30 duty_cycle = signed_duty_cycle/100.0;
LanierUSNA16 1:d4c2e72540db 31
LanierUSNA16 1:d4c2e72540db 32 printf("\n %f duty cycle set. Enter 1 for CCW or 0 for CW spin direction:\n",duty_cycle);
LanierUSNA16 0:8d7b6e55609a 33 scanf("%d", &user_direction);
LanierUSNA16 0:8d7b6e55609a 34
LanierUSNA16 0:8d7b6e55609a 35 printf("it's starting... move the thing!\n");
LanierUSNA16 0:8d7b6e55609a 36 t.start();
LanierUSNA16 0:8d7b6e55609a 37 turret_speed = duty_cycle;
LanierUSNA16 0:8d7b6e55609a 38 turret_direction = user_direction;
LanierUSNA16 1:d4c2e72540db 39 printf("%f duty cycle. %d spin.\n", duty_cycle, user_direction);
LanierUSNA16 0:8d7b6e55609a 40
LanierUSNA16 1:d4c2e72540db 41 while(1)
LanierUSNA16 1:d4c2e72540db 42 {
LanierUSNA16 1:d4c2e72540db 43 wait(0.01);
LanierUSNA16 1:d4c2e72540db 44 pulses = myEncoder.getPulses();
LanierUSNA16 1:d4c2e72540db 45 theta = ((float)pulses/ (1600.0*2.0))*-2.0*3.14;
LanierUSNA16 1:d4c2e72540db 46 time = t.read();
LanierUSNA16 0:8d7b6e55609a 47
LanierUSNA16 1:d4c2e72540db 48 signed_duty_cycle = Kp*(theta_ref - theta);
LanierUSNA16 1:d4c2e72540db 49 duty_cycle = signed_duty_cycle;
LanierUSNA16 1:d4c2e72540db 50 turret_speed = duty_cycle;
LanierUSNA16 1:d4c2e72540db 51
LanierUSNA16 1:d4c2e72540db 52 printf("time = %.5f;\n",time);
LanierUSNA16 1:d4c2e72540db 53 printf("theta = %.5f;\n",theta);
LanierUSNA16 1:d4c2e72540db 54 printf("duty cyle= %f. \n", duty_cycle);
LanierUSNA16 1:d4c2e72540db 55 }
LanierUSNA16 0:8d7b6e55609a 56 }
LanierUSNA16 0:8d7b6e55609a 57 }