Simple program to control pololu motor with button (direction) and potmeter (velocity)

Dependencies:   MODSERIAL mbed

Committer:
GerhardBerman
Date:
Mon Oct 10 10:06:57 2016 +0000
Revision:
0:8f8157690923
motor working, potmeter not working;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GerhardBerman 0:8f8157690923 1 #include "mbed.h"
GerhardBerman 0:8f8157690923 2 #include <math.h>
GerhardBerman 0:8f8157690923 3 #include "MODSERIAL.h"
GerhardBerman 0:8f8157690923 4
GerhardBerman 0:8f8157690923 5 AnalogIn potMeterIn(A0);
GerhardBerman 0:8f8157690923 6 DigitalIn button1(D7);
GerhardBerman 0:8f8157690923 7 DigitalOut motor1DirectionPin(D4);
GerhardBerman 0:8f8157690923 8 PwmOut motor1MagnitudePin(D5);
GerhardBerman 0:8f8157690923 9
GerhardBerman 0:8f8157690923 10 Serial pc(USBTX,USBRX);
GerhardBerman 0:8f8157690923 11 Ticker MeasureTicker;
GerhardBerman 0:8f8157690923 12
GerhardBerman 0:8f8157690923 13 volatile float referenceVelocity = 0; // in rad/s
GerhardBerman 0:8f8157690923 14
GerhardBerman 0:8f8157690923 15 float GetReferenceVelocity()
GerhardBerman 0:8f8157690923 16 {
GerhardBerman 0:8f8157690923 17 // Returns reference velocity in rad/s.
GerhardBerman 0:8f8157690923 18 // Positive value means clockwise rotation.
GerhardBerman 0:8f8157690923 19 const float maxVelocity=8.4; // in rad/s of course!
GerhardBerman 0:8f8157690923 20
GerhardBerman 0:8f8157690923 21 if (button1)
GerhardBerman 0:8f8157690923 22 {
GerhardBerman 0:8f8157690923 23 // Clockwise rotation
GerhardBerman 0:8f8157690923 24 referenceVelocity = potMeterIn * maxVelocity;
GerhardBerman 0:8f8157690923 25 } else
GerhardBerman 0:8f8157690923 26 {
GerhardBerman 0:8f8157690923 27 // Counterclockwise rotation
GerhardBerman 0:8f8157690923 28 referenceVelocity = -1*potMeterIn * maxVelocity;
GerhardBerman 0:8f8157690923 29 }
GerhardBerman 0:8f8157690923 30 return referenceVelocity;
GerhardBerman 0:8f8157690923 31 }
GerhardBerman 0:8f8157690923 32
GerhardBerman 0:8f8157690923 33 float FeedForwardControl(float referenceVelocity)
GerhardBerman 0:8f8157690923 34 {
GerhardBerman 0:8f8157690923 35 // very simple linear feed-forward control
GerhardBerman 0:8f8157690923 36 const float MotorGain=8.4; // unit: (rad/s) / PWM
GerhardBerman 0:8f8157690923 37 volatile float motorValue = referenceVelocity / MotorGain;
GerhardBerman 0:8f8157690923 38 return motorValue;
GerhardBerman 0:8f8157690923 39 }
GerhardBerman 0:8f8157690923 40
GerhardBerman 0:8f8157690923 41 void SetMotor1(float motorValue)
GerhardBerman 0:8f8157690923 42 {
GerhardBerman 0:8f8157690923 43 // Given -1<=motorValue<=1, this sets the PWM and direction
GerhardBerman 0:8f8157690923 44 // bits for motor 1. Positive value makes motor rotating
GerhardBerman 0:8f8157690923 45 // clockwise. motorValues outside range are truncated to
GerhardBerman 0:8f8157690923 46 // within range
GerhardBerman 0:8f8157690923 47 if (motorValue >=0) motor1DirectionPin=1;
GerhardBerman 0:8f8157690923 48 else motor1DirectionPin=0;
GerhardBerman 0:8f8157690923 49 if (fabs(motorValue)>1) motor1MagnitudePin = 1;
GerhardBerman 0:8f8157690923 50 else motor1MagnitudePin = fabs(motorValue);
GerhardBerman 0:8f8157690923 51 }
GerhardBerman 0:8f8157690923 52
GerhardBerman 0:8f8157690923 53 void MeasureAndControl(void)
GerhardBerman 0:8f8157690923 54 {
GerhardBerman 0:8f8157690923 55 // This function measures the potmeter position, extracts a
GerhardBerman 0:8f8157690923 56 // reference velocity from it, and controls the motor with
GerhardBerman 0:8f8157690923 57 // a simple FeedForward controller. Call this from a Ticker.
GerhardBerman 0:8f8157690923 58 volatile float referenceVelocity = GetReferenceVelocity();
GerhardBerman 0:8f8157690923 59 float motorValue = FeedForwardControl(referenceVelocity);
GerhardBerman 0:8f8157690923 60 SetMotor1(motorValue);
GerhardBerman 0:8f8157690923 61 }
GerhardBerman 0:8f8157690923 62
GerhardBerman 0:8f8157690923 63
GerhardBerman 0:8f8157690923 64 int main()
GerhardBerman 0:8f8157690923 65 {
GerhardBerman 0:8f8157690923 66
GerhardBerman 0:8f8157690923 67 //Initialize
GerhardBerman 0:8f8157690923 68 MeasureTicker.attach(&MeasureAndControl, 1.0/100);
GerhardBerman 0:8f8157690923 69 pc.printf("Reference velocity: %f rad/s \r\n", referenceVelocity);
GerhardBerman 0:8f8157690923 70 pc.baud(115200);
GerhardBerman 0:8f8157690923 71 while(true)
GerhardBerman 0:8f8157690923 72 {}
GerhardBerman 0:8f8157690923 73 }