code

Dependencies:   FastPWM HIDScope mbed

Fork of Motor_Rotator by Eki Liptiay

Committer:
Iriskolenbrander9
Date:
Thu Sep 27 14:06:44 2018 +0000
Revision:
2:328fa2eb6437
Parent:
1:aab087815027
Motor 1 working, hidscope is not.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ekiliptiay 0:5746f50d8ee4 1 #include "mbed.h"
ekiliptiay 0:5746f50d8ee4 2 #include "HIDScope.h"
ekiliptiay 0:5746f50d8ee4 3
Iriskolenbrander9 2:328fa2eb6437 4 // Motor 1 has PWM-pin D6 and direction-pin D7
ekiliptiay 0:5746f50d8ee4 5
ekiliptiay 0:5746f50d8ee4 6 AnalogIn pot1(A0);
Iriskolenbrander9 1:aab087815027 7 DigitalOut directionpin(D7);
Iriskolenbrander9 1:aab087815027 8 PwmOut pwmpin(D6);
ekiliptiay 0:5746f50d8ee4 9 HIDScope scope(1); // We’re going to send 1 channel of data
ekiliptiay 0:5746f50d8ee4 10 Ticker AInTicker;
ekiliptiay 0:5746f50d8ee4 11
Iriskolenbrander9 2:328fa2eb6437 12 void ReadAnalogInAndFilter(){
Iriskolenbrander9 2:328fa2eb6437 13 float a = 2.0f*pot1-1.0f; //value of the potmeter is rescaled between -1 and 1
Iriskolenbrander9 2:328fa2eb6437 14 scope.set(0, a); // save scaled potmeter value
ekiliptiay 0:5746f50d8ee4 15 scope.send(); // send what's in scope memory to PC
ekiliptiay 0:5746f50d8ee4 16 }
ekiliptiay 0:5746f50d8ee4 17
Iriskolenbrander9 2:328fa2eb6437 18 void SetMotorSpeed(){
Iriskolenbrander9 2:328fa2eb6437 19 float u = 2.0f*pot1-1.0f;
Iriskolenbrander9 2:328fa2eb6437 20 directionpin = u > 0.0f; //either true or false, output 0 or 1
Iriskolenbrander9 2:328fa2eb6437 21 pwmpin = fabs(u); //pwm duty cycle can only be positive, floating point absolute value of the potmeter value
Iriskolenbrander9 2:328fa2eb6437 22 }
Iriskolenbrander9 2:328fa2eb6437 23
ekiliptiay 0:5746f50d8ee4 24 int main(){
ekiliptiay 0:5746f50d8ee4 25 pwmpin.period_us(60);
Iriskolenbrander9 2:328fa2eb6437 26 while(true){
ekiliptiay 0:5746f50d8ee4 27 AInTicker.attach(&ReadAnalogInAndFilter, 0.01f);
Iriskolenbrander9 2:328fa2eb6437 28 SetMotorSpeed();
ekiliptiay 0:5746f50d8ee4 29 }
ekiliptiay 0:5746f50d8ee4 30 }