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: MODSERIAL mbed HIDScope QEI
Fork of prog_pract3 by
main.cpp
- Committer:
- Marieke
- Date:
- 2016-10-10
- Revision:
- 3:c1ef4d7490c1
- Parent:
- 2:ee821b9bf42b
- Child:
- 4:db3e61625e18
File content as of revision 3:c1ef4d7490c1:
#include "mbed.h"
#include <math.h>
#include "MODSERIAL.h"
DigitalOut led1 (D12);
DigitalOut led2 (D10);
AnalogIn potMeterIn(PTB2);
DigitalIn button1(D7);
DigitalOut motor1DirectionPin(D6);
PwmOut motor1MagnitudePin(D7);
Serial pc(USBTX,USBRX);
Ticker MeasureTicker, TimeTracker;
float referenceVelocity = 0;
volatile bool MeasureTicker_go=false, TimeTracker_go=false;
void MeasureTicker_act(){MeasureTicker_go=true;}; // Activates go-flags
void TimeTracker_act(){TimeTracker_go=true;};
float GetReferenceVelocity()
{
// Returns reference velocity in rad/s.
// Positive value means clockwise rotation.
const float maxVelocity = 8.4; // in rad/s of course!
if (button1 == 0){
led1=1;
led2=0;
// Clockwise rotation
referenceVelocity = potMeterIn * maxVelocity;
}
else {
led1=0;
led2=1;
// Counterclockwise rotation
referenceVelocity = -1*potMeterIn * maxVelocity;
}
return referenceVelocity;
}
float FeedForwardControl(float referenceVelocity)
{
// very simple linear feed-forward control
const float MotorGain=8.4; // unit: (rad/s) / PWM
float motorValue = referenceVelocity / MotorGain;
return motorValue;
}
void SetMotor1(float motorValue)
{
// Given -1<=motorValue<=1, this sets the PWM and direction
// bits for motor 1. Positive value makes motor rotating
// clockwise. motorValues outside range are truncated to
// within range
if (motorValue >=0) motor1DirectionPin=1;
else motor1DirectionPin=0;
if (fabs(motorValue)>1) motor1MagnitudePin = 1;
else motor1MagnitudePin = fabs(motorValue);
}
void MeasureAndControl(void)
{
// This function measures the potmeter position, extracts a
// reference velocity from it, and controls the motor with
// a simple FeedForward controller. Call this from a Ticker.
float referenceVelocity = GetReferenceVelocity();
float motorValue = FeedForwardControl(referenceVelocity);
SetMotor1(motorValue);
pc.printf("MotorValue: %f rad/s \r\n", motorValue);
}
void TimeTrackerF(){
wait(1);
float Potmeter = potMeterIn.read();
pc.printf("Reference velocity: %f rad/s \r\n", referenceVelocity);
pc.printf("Potmeter: %f rad/s \r\n", Potmeter);
}
int main()
{
//Initialize
led1=0;
led2=0;
float Potmeter = potMeterIn.read();
MeasureTicker.attach(&MeasureTicker_act, 0.01f);
TimeTracker.attach(&TimeTracker_act, 0.1f);
pc.baud(115200);
pc.printf("Reference velocity: %f rad/s \r\n", referenceVelocity);
pc.printf("Potmeter: %f rad/s \r\n", Potmeter);
while(1)
{
if (MeasureTicker_go){
MeasureTicker_go=false;
MeasureAndControl();
}
if (TimeTracker_go){
TimeTracker_go=false;
TimeTrackerF();
}
}
}
