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 6:0832c6c6bcba, committed 2014-09-29
- Comitter:
- vsluiter
- Date:
- Mon Sep 29 21:39:29 2014 +0000
- Parent:
- 5:06381e54b94a
- Child:
- 7:37b06688b449
- Commit message:
- Removed Serial references. Added HIDScope and arm math libraries
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
| mbed-dsp.lib | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Mon Sep 29 21:20:29 2014 +0000
+++ b/main.cpp Mon Sep 29 21:39:29 2014 +0000
@@ -1,20 +1,23 @@
#include "mbed.h"
#include "encoder.h"
-#include "MODSERIAL.h"
+//#include "MODSERIAL.h"
+#include "HIDScope.h"
+
#define TSAMP 0.01
#define K_P (10 *TSAMP)
-
#define K_I (0.00 *TSAMP)
-
#define K_D (5 *TSAMP)
#define POT_AVG 50
+
void coerce(float * in, float min, float max);
float pid(float setpoint, float measurement);
AnalogIn potmeter(PTC2);
volatile bool looptimerflag;
float potsamples[POT_AVG];
+HIDScope scope(4);
+
void setlooptimerflag(void)
{
@@ -46,16 +49,19 @@
int main()
{
+ //start Encoder
Encoder motor1(PTD0,PTC9);
+ //Ticker to average potmeter values
Ticker potaverage;
- MODSERIAL pc(USBTX,USBRX);
+ //MODSERIAL pc(USBTX,USBRX);
PwmOut pwm_motor(PTA12);
+ //10kHz PWM frequency
+ pwm_motor.period_us(100);
DigitalOut motordir(PTD3);
potaverage.attach(potAverager,0.002);
- pc.baud(921600);
+ //pc.baud(921600);
Ticker looptimer;
looptimer.attach(setlooptimerflag,TSAMP);
- pc.printf("bla");
while(1) {
float setpoint;
float new_pwm;
@@ -65,7 +71,11 @@
//new_pwm = (setpoint - motor1.getPosition())*.001;
new_pwm = pid(setpoint, motor1.getPosition());
coerce(&new_pwm, -1,1);
- pc.printf("%f %f %f 0\n", setpoint/2000.+0.5, motor1.getPosition()/2000.+.5,new_pwm/2.+0.5);
+ scope.set(0, setpoint);
+ scope.set(1,new_pwm);
+ // ch 2 and 3 set in pid controller */
+ scope.send();
+ //pc.printf("%f %f %f 0\n", setpoint/2000.+0.5, motor1.getPosition()/2000.+.5,new_pwm/2.+0.5);
if(new_pwm > 0)
motordir = 1;
else
@@ -80,7 +90,7 @@
//'ternary operators'.
void coerce(float * in, float min, float max)
{
-*in > min ? *in < max? : *in = max: *in = min;
+ *in > min ? *in < max? : *in = max: *in = min;
}
@@ -97,6 +107,8 @@
out_d = (error-prev_error)*K_D;
coerce(&out_i,-0.5,0.5);
prev_error = error;
+ scope.set(2,error);
+ scope.set(3,out_p);
return out_p + out_i + out_d;
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-dsp.lib Mon Sep 29 21:39:29 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-dsp/#9cee975aadce
