Demo of combination HIDScope and encoder

Dependencies:   Encoder HIDScope mbed

Committer:
vsluiter
Date:
Thu Jun 23 11:33:22 2016 +0000
Revision:
1:1db5c1c28a93
Parent:
0:0b39982f89a9
New version with analog input

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vsluiter 0:0b39982f89a9 1 #include "mbed.h"
vsluiter 0:0b39982f89a9 2 #include "HIDScope.h"
vsluiter 0:0b39982f89a9 3 #include "encoder.h"
vsluiter 0:0b39982f89a9 4
vsluiter 0:0b39982f89a9 5 Encoder motor1(D13,D12);
vsluiter 0:0b39982f89a9 6 PwmOut led(D9);
vsluiter 1:1db5c1c28a93 7 HIDScope scope(3);
vsluiter 1:1db5c1c28a93 8
vsluiter 1:1db5c1c28a93 9
vsluiter 0:0b39982f89a9 10
vsluiter 0:0b39982f89a9 11 int main()
vsluiter 0:0b39982f89a9 12 {
vsluiter 1:1db5c1c28a93 13 const float degrees_per_count = 90/200.0; //quick approach
vsluiter 1:1db5c1c28a93 14 const float ain_degrees_range = 90; //degrees over which the potmeter can control.
vsluiter 1:1db5c1c28a93 15 const float kp_1_range = -1;
vsluiter 1:1db5c1c28a93 16 AnalogIn setpoint_1(A0);
vsluiter 1:1db5c1c28a93 17 AnalogIn p_value(A1);
vsluiter 1:1db5c1c28a93 18 DigitalOut direction_1(D4);
vsluiter 1:1db5c1c28a93 19 PwmOut pwm_1(D5);
vsluiter 1:1db5c1c28a93 20 float degrees_setpoint;
vsluiter 1:1db5c1c28a93 21 float error;
vsluiter 1:1db5c1c28a93 22 float output;
vsluiter 1:1db5c1c28a93 23 pwm_1.period(1.0/10000); //10kHz
vsluiter 1:1db5c1c28a93 24
vsluiter 0:0b39982f89a9 25 while (true) {
vsluiter 1:1db5c1c28a93 26 degrees_setpoint = setpoint_1 * ain_degrees_range;
vsluiter 1:1db5c1c28a93 27 error = degrees_setpoint-(motor1.getPosition()*degrees_per_count); //error = setpoint - current
vsluiter 1:1db5c1c28a93 28 output = error*p_value*kp_1_range;
vsluiter 1:1db5c1c28a93 29 if(output > 0)
vsluiter 1:1db5c1c28a93 30 {
vsluiter 1:1db5c1c28a93 31 direction_1.write(true);
vsluiter 1:1db5c1c28a93 32 }
vsluiter 1:1db5c1c28a93 33 else
vsluiter 1:1db5c1c28a93 34 {
vsluiter 1:1db5c1c28a93 35 direction_1.write(false);
vsluiter 1:1db5c1c28a93 36 }
vsluiter 1:1db5c1c28a93 37 pwm_1.write(fabs(output));
vsluiter 0:0b39982f89a9 38 scope.set(0,motor1.getPosition());
vsluiter 1:1db5c1c28a93 39 scope.set(1,error);
vsluiter 1:1db5c1c28a93 40 scope.set(2,fabs(output));
vsluiter 0:0b39982f89a9 41 led.write(motor1.getPosition()/100.0);
vsluiter 0:0b39982f89a9 42 scope.send();
vsluiter 1:1db5c1c28a93 43 wait(0.01);
vsluiter 0:0b39982f89a9 44 }
vsluiter 0:0b39982f89a9 45 }