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: mbed
Diff: WheelControl/Wheel.h
- Revision:
- 3:01b5e80d842d
- Parent:
- 1:813f4b17ae65
diff -r 730ccfbf08d5 -r 01b5e80d842d WheelControl/Wheel.h
--- a/WheelControl/Wheel.h Sun Mar 03 00:55:10 2019 +0000
+++ b/WheelControl/Wheel.h Sat Mar 09 14:27:48 2019 +0000
@@ -5,7 +5,7 @@
float distance; //distance traversed by wheel
float angularVelocity;
- float const static gain = 0.6f; //closed loop gain, (amount to amplify the difference) you have to tune this value
+ float const static gain = 1.2f; //closed loop gain, (amount to amplify the difference) you have to tune this value
//but make sure its less than 1.5 otherwise you'll have a really sensitive motor
PwmOut Mtr; //connect this pin to the motor driveboard pwm
@@ -20,31 +20,30 @@
public:
- float maxAngularVel;
+ float maxAngularVel;
float static const wheelDiameter = 0.18; //used in calculation of Linear velocity i.e never
- Wheel (Encoder* E, PinName M, PinName D, PinName Mode) : Mtr(M), direction(D), polarity(Mode), controller(0.6f)
+ Wheel (Encoder* E, PinName M, PinName D, PinName Mode) : Mtr(M), direction(D), polarity(Mode), controller(gain)
{
+ maxAngularVel = 0.0f;
enc = E;
polarity = 0;
direction = 0;
distance = 0;
+ Mtr.period_us(100); //frequency of 5KHz determine this constant value based on switching losses+frequency losses
+ //higher freq -> more switching losses lower freq -> more "capacitive losses" need to find a balance
+ Mtr.write(1); //start off on the turned off state
- Mtr.period_us(200); //frequency of 1KHz determine this constant value based on switching losses+frequency losses
- //higher freq -> more switching losses lower freq -> more "capacitive losses" need to find a balance
updater.detach();
- controller.setControl(10.0f);
controller.setOutputLimits(-1.0f, 1.0f);
}
- float calculateAngularVelocity() //returns a float value which is the angular velocity of the WHEEL
+ void calculateAngularVelocity() //returns a float value which is the angular velocity of the WHEEL
{
- float eTR;
- eTR = enc->encoderTickRate();
+ float eTR = enc->encoderTickRate();
angularVelocity = (eTR/256.0f)*2.0f*(float)PI;
- return eTR;
}
void setFrequency(int freq) //if you want to adjust the frequency
@@ -61,33 +60,34 @@
//dir = direction, do opposite for each wheel just so your buggy doesn't move FORWARD but rather rotates
void init(int dir)
{
+ enc->startTimer();
Mtr.write(0); //max speed
+ angularVelocity = 10.0f;
direction = dir;
- updater.attach(callback(this, &Wheel::init2),1); //used as a wait preferably put this wait just long enough that the buggy will do a full 360 degree turn so that it hasn't moved
+ updater.detach();
+ updater.attach(callback(this, &Wheel::init2),2.0f); //used as a wait preferably put this wait just long enough that the buggy will do a full 360 degree turn so that it hasn't moved
}
void init2(void) //used as a temporarily wait command for the wheel to spin to max
{
- float temp = enc->encoderTickRate();
- angularVelocity = (temp/256.0f)*2.0f*(float)PI;
- maxAngularVel = angularVelocity;
- controller.setInputLimits(-1.0f*angularVelocity,angularVelocity);
- updater.attach(callback(this, &Wheel::wheelUpdates),0.1); //attached the actual update function from now ON
+ calculateAngularVelocity();
+ maxAngularVel = abs(angularVelocity);
+ controller.setInputLimits(-1.0f*abs(angularVelocity),abs(angularVelocity));
+ controller.setControl(0.0f);
+ updater.detach();
+ updater.attach(callback(this, &Wheel::wheelUpdates),0.2f); //attached the actual update function from now ON
}
void wheelUpdates(void) //sampling rate the ticker is attached I.E the wheel speed is updated everytiem this function is called
{
- if (angularVelocity >= (controller.returnControl()+1.0f) || angularVelocity <= (controller.returnControl()-1.0f)) //only compute if their is a difference between wanted and current val
- {
- float temp = enc->encoderTickRate(); //get the encoder tick rate and store in a value called temp
- angularVelocity = (temp/256.0f)*2.0f*(float)PI; //use it to calculate the angular velocity of the wheel
+ calculateAngularVelocity();
float temp2 = controller.compute(angularVelocity); //another temporary value to store the computed angular velocity
if (temp2 < 0) {direction = 1;} else {direction = 0;} //change direction according to the computed value
- Mtr.write(temp2); //write the value as a pwm
- distance += angularVelocity * wheelDiameter;
- }
+ temp2 = abs(temp2);
+ Mtr.write(1.0f - temp2); //write the value as a pwm
}
- void adjustAngularVelocity(float W) // W = angular velocity you want, obviously putting a |w| value that is > max angular velocity will set dutcy cycle to 1
+
+ void adjustAngularVelocity(float W) // W = angular velocity you want, obviously putting a |w| value that is > max angular velocity will set dutcy cycle to max
{
controller.setControl(W);
if (W < 0.0f) {direction = 1;} else {direction = 0;} //obvs if you put a negative value -> will get a negative direction i.e 0;
@@ -98,4 +98,10 @@
return distance; //distance traversed by wheel
}
+ float returnMaxAngularVel(void)
+ {
+ return maxAngularVel;
+ }
+
+
};
\ No newline at end of file