Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Encoder MODSERIAL mbed-dsp mbed
Fork of motor1aansturing by
Diff: main.cpp
- Revision:
- 2:ef0fa691e77e
- Parent:
- 0:eb00992c1597
- Child:
- 3:864137a5f702
--- a/main.cpp Fri Oct 03 09:01:41 2014 +0000 +++ b/main.cpp Fri Oct 03 10:15:30 2014 +0000 @@ -1,21 +1,19 @@ #include "mbed.h" #include "encoder.h" -#include "HIDScope.h" - #define TSAMP 0.01 #define K_P (0.01) #define K_I (0 *TSAMP) -#define K_D (0 /TSAMP) +#define K_D (0 /TSAMP) #define I_LIMIT 1. #define POT_AVG 50 void clamp(float * in, float min, float max); float pid(float setpoint, float measurement); +//-------------------------------------------------------------------------------------------Input potentiometer------------------------------- AnalogIn potmeter(PTC2); volatile bool looptimerflag; float potsamples[POT_AVG]; -HIDScope scope(6); void setlooptimerflag(void) @@ -25,11 +23,8 @@ int main() { - //start Encoder Encoder motor1(PTD0,PTC9); - /*PwmOut to motor driver*/ PwmOut pwm_motor(PTA5); - //10kHz PWM frequency pwm_motor.period_us(100); DigitalOut motordir(PTD1); Ticker looptimer; @@ -37,31 +32,23 @@ while(1) { float setpoint; float new_pwm; - /*wait until timer has elapsed*/ while(!looptimerflag); - looptimerflag = false; //clear flag - /*potmeter value: 0-1*/ - setpoint = (potmeter.read()-.5)*500; - /*new_pwm = (setpoint - motor1.getPosition())*.001; -> P action*/ + looptimerflag = false; + //---------------------------------------------------------leest potentiometer (?), schaalt tussen 0 en 1------------------------------------ + setpoint = (potmeter.read()-.5)*500; + //----------------------------------------------------------------------new_pwm = getal?.....------------------------------------------------ new_pwm = pid(setpoint, motor1.getPosition()); clamp(&new_pwm, -1,1); - scope.set(0, setpoint); - scope.set(4, new_pwm); - scope.set(5, motor1.getPosition()); - // ch 1, 2 and 3 set in pid controller */ - scope.send(); + //-------------------------------------------------------------------------------------output motor richting---------------------------------- if(new_pwm > 0) motordir = 0; else motordir = 1; + //-------------------------------------------------------------------------------output motorsnelhied (wij willen 4 distinctive stappen)------ pwm_motor.write(abs(new_pwm)); } } - -//clamps value 'in' to min or max when exceeding those values -//if you'd like to understand the statement below take a google for -//'ternary operators'. void clamp(float * in, float min, float max) { *in > min ? *in < max? : *in = max: *in = min; @@ -81,8 +68,5 @@ out_d = (error-prev_error)*K_D; clamp(&out_i,-I_LIMIT,I_LIMIT); prev_error = error; - scope.set(1,out_p); - scope.set(2,out_i); - scope.set(3,out_d); return out_p + out_i + out_d; }