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 HIDScope MODSERIAL mbed-dsp mbed
Fork of BMT-M9_motorcontrol by
Revision 13:7ec4762ec4ce, committed 2014-10-17
- Comitter:
- Hooglugt
- Date:
- Fri Oct 17 08:47:27 2014 +0000
- Parent:
- 11:4f65e70290ac
- Child:
- 14:98925c1ed440
- Commit message:
- onnodige stukken code verwijdert voor testen motor (step function)
Changed in this revision
| MODSERIAL.lib | Show annotated file Show diff for this revision Revisions of this file |
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MODSERIAL.lib Fri Oct 17 08:47:27 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/Sissors/code/MODSERIAL/#2e4e3795a093
--- a/main.cpp Wed Oct 15 13:57:45 2014 +0000
+++ b/main.cpp Fri Oct 17 08:47:27 2014 +0000
@@ -3,92 +3,50 @@
#include "HIDScope.h"
#define TSAMP 0.01
-#define K_P (0.01)
-#define K_I (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);
-AnalogIn potmeter(PTC2);
volatile bool looptimerflag;
-float potsamples[POT_AVG];
HIDScope scope(2);
-
void setlooptimerflag(void)
{
looptimerflag = true;
}
-
+void clamp(float * in, float min, float max)
+{
+*in > min ? *in < max? : *in = max: *in = min;
+}
int main()
{
- //start Encoder
- Encoder motor1(PTD0,PTC9);
- // @param int_a Pin to be used as InterruptIn! Be careful, as not all pins on all platforms may be used as InterruptIn.
- // @param int_b second encoder pin, used as DigitalIn. Can be any DigitalIn pin, not necessarily on InterruptIn location
- /*PwmOut to motor driver*/
- PwmOut pwm_motor(PTA5);
- //10kHz PWM frequency
- pwm_motor.period_us(100);
- DigitalOut motordir(PTD1);
+ Encoder motor1(PTD2,PTD0);
+ PwmOut pwm_motor(PTC8);
+ pwm_motor.period_us(100); //10kHz PWM frequency
+ DigitalOut motordir(PTC9);
Ticker looptimer;
looptimer.attach(setlooptimerflag,TSAMP);
+
+ wait(10);
+
while(1) {
- float setpoint;
- float new_pwm;
+ float new_pwm;
+
while(!looptimerflag);
looptimerflag = false; //clear flag
- //potmeter value: 0-1
- //setpoint = (potmeter.read()-.5)*500;
- //new_pwm = (setpoint - motor1.getPosition())*.001; -> P action
- new_pwm = 1; // DIT IS PUUR VOOR DE STEPFUNCTION
- //new_pwm = pid(setpoint, motor1.getPosition());
+
+ new_pwm = 1;
clamp(&new_pwm, -1,1);
- //scope.set(0, setpoint);
- scope.set(1, new_pwm);
- scope.set(2, motor1.getPosition());
- // ch 1, 2 and 3 set in pid controller
+
+ scope.set(0, new_pwm);
+ scope.set(1, motor1.getPosition());
scope.send();
+
if(new_pwm > 0)
motordir = 0;
else
motordir = 1;
+
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;
-}
-
-/*
-float pid(float setpoint, float measurement)
-{
- float error;
- static float prev_error = 0;
- float out_p = 0;
- static float out_i = 0;
- float out_d = 0;
- error = setpoint-measurement;
- out_p = error*K_P;
- out_i += error*K_I;
- 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;
-}
-*/
-
+}
\ No newline at end of file
