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
main.cpp
- Committer:
- Hooglugt
- Date:
- 2014-10-17
- Revision:
- 13:7ec4762ec4ce
- Parent:
- 11:4f65e70290ac
- Child:
- 14:98925c1ed440
File content as of revision 13:7ec4762ec4ce:
#include "mbed.h"
#include "encoder.h"
#include "HIDScope.h"
#define TSAMP 0.01
void clamp(float * in, float min, float max);
volatile bool looptimerflag;
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()
{
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 new_pwm;
while(!looptimerflag);
looptimerflag = false; //clear flag
new_pwm = 1;
clamp(&new_pwm, -1,1);
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));
}
}
