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: BMT-K9-Regelaar Encoder MODSERIAL mbed
Fork of BMT-K9-Regelaar by
main.cpp
- Committer:
- vsluiter
- Date:
- 2013-10-09
- Revision:
- 1:9d05c0236c7e
- Parent:
- 0:7bc93f851767
- Child:
- 2:3d4a843be2a9
File content as of revision 1:9d05c0236c7e:
#include "mbed.h"
#include "encoder.h"
#include "MODSERIAL.h"
/** keep_in_range -> float in, and keep_in_range if less than min, or larger than max **/
void keep_in_range(float * in, float min, float max);
volatile bool looptimerflag;
void setlooptimerflag(void)
{
looptimerflag = true;
}
int main() {
//LOCAL VARIABLES
AnalogIn potmeter(PTC2);
Encoder motor1(PTD0,PTC9);//first pin on PTAx or PTDx
MODSERIAL pc(USBTX,USBRX);
PwmOut pwm_motor(PTA12);
DigitalOut motordir(PTD3);
float setpoint;
float pwm_to_motor;
//START OF CODE
pc.baud(230400);
Ticker looptimer;
looptimer.attach(setlooptimerflag,0.01);
pc.printf("bla");
//INFINITE LOOP
while(1) {
while(looptimerflag != true);
looptimerflag = false;
setpoint = (potmeter.read()-0.5)*2000;
pc.printf("s: %f, %d \n\r", setpoint, motor1.getPosition());
pwm_to_motor = (setpoint - motor1.getPosition())*.001;
keep_in_range(&pwm_to_motor, -1,1);
if(pwm_to_motor > 0)
motordir = 0;
else
motordir = 1;
//WRITE VALUE TO MOTOR
pwm_motor.write(abs(pwm_to_motor));
}
}
//coerces 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 keep_in_range(float * in, float min, float max)
{
*in > min ? *in < max? : *in = max: *in = min;
}
