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.
Revision 4:48d390356fba, committed 2018-02-22
- Comitter:
- Weranest
- Date:
- Thu Feb 22 17:01:00 2018 +0000
- Parent:
- 3:c9df852ad9ac
- Commit message:
- Tech Demo version
Changed in this revision
--- a/constants.cpp Wed Feb 21 11:55:29 2018 +0000 +++ b/constants.cpp Thu Feb 22 17:01:00 2018 +0000 @@ -9,4 +9,5 @@ #define SLOW_PWM 0.3f #define RIGHT_MOTOR_CONST 1 #define LEFT_MOTOR_CONST 1 -#define MOTOR_PERIOD 1.0f \ No newline at end of file +#define MOTOR_PERIOD 1.0f +#define ADC_CONSTANT 0.0005f \ No newline at end of file
--- a/constants.h Wed Feb 21 11:55:29 2018 +0000 +++ b/constants.h Thu Feb 22 17:01:00 2018 +0000 @@ -2,9 +2,12 @@ extern int GEAR_RATIO; extern float SAMPLE_TIME; extern int WHEEL_DISTANCE; -extern float NORMAL_PWM; -extern float FAST_PWM; -extern float SLOW_PWM; +extern float NORMAL_PWM_L; +extern float NORMAL_PWM_R; +extern float FAST_PWM_L; +extern float FAST_PWM_R; +extern float SLOW_PWM_L; +extern float SLOW_PWM_R; extern int RIGHT_MOTOR_CONST; extern int LEFT_MOTOR_CONST; extern float MOTOR_PERIOD; \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/control.cpp Thu Feb 22 17:01:00 2018 +0000
@@ -0,0 +1,72 @@
+#include "mbed.h"
+#include "pwm.h"
+#include "pins.h"
+#include "constants.cpp"
+#include "C12832.h"
+#include "encoderBase.h"
+
+void buggyGoLeft();
+void buggyGoF();
+void buggyGoRight();
+void buggyStop();
+
+void controlAutomatic(int flag){ //this is a showoff version
+ while(flag==1){
+ wait(0.5);
+ buggyGoF();
+ wait(2);
+ buggyGoLeft();
+ wait(2);
+ buggyGoRight();
+ wait(2);
+ buggyStop();
+ }
+}
+void controlManual(int flag){
+ while (1){ //this function is for manual wheel turnage using the potentiometers. Knock yourself out
+ while(1){
+ if(joystickDown!=0) break;
+
+ float rightPWM= (float)potentiometerRight.read() ;
+ float leftPWM= (float)potentiometerLeft.read();
+ lcd.locate(0,3);
+ lcd.printf("L=%d; R=%d", encoderVelocityLeft(), encoderVelocityRight());
+ //wait(2);
+ lcd.cls();
+ buggyPWM(leftPWM ,rightPWM);
+ }
+ wait(0.5);
+ buggyGoF();
+ wait(1);
+ buggyGoLeft();
+ wait(2);
+ buggyGoRight();
+ wait(2);
+ buggyGoF();
+ wait (1);
+ buggyStop();
+ }
+ }
+
+
+void stateMachine(int flag){
+ while (1){ //depending on the flag changes the state. Not efficient, since relies on looping the whole thing, but will do for now
+ switch(flag){
+ case 0: controlManual(flag);
+ case 1:controlAutomatic(flag);
+ break;
+ }
+ }
+ }
+void buggyGoLeft(){
+ buggyPWM(SLOW_PWM, FAST_PWM);
+ }
+void buggyGoRight(){
+ buggyPWM(FAST_PWM, SLOW_PWM);
+ }
+void buggyGoF(){
+ buggyPWM(NORMAL_PWM, NORMAL_PWM);
+ }
+void buggyStop(){
+ buggyPWM(0.0f, 0.0f);
+ }
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/control.h Thu Feb 22 17:01:00 2018 +0000 @@ -0,0 +1,5 @@ +extern void buggyGoF(); +extern void buggyGoLeft(); +extern void buggyGoRight(); +extern void buggyStop(); +extern void controlManual(int); \ No newline at end of file
--- a/encoderBase.cpp Wed Feb 21 11:55:29 2018 +0000
+++ b/encoderBase.cpp Thu Feb 22 17:01:00 2018 +0000
@@ -1,7 +1,7 @@
#include "mbed.h"
#include "QEI.h"
#include "pins.h"
-#include "constants.h"
+#include "constants.cpp"
//these lines setup the encoders for both wheels
@@ -33,3 +33,10 @@
return (wheelRight.velocity(pulseRight)-wheelLeft.velocity(pulseLeft))/WHEEL_DISTANCE;
}
}buggy;
+ //temp functions to get the encoder speed
+int encoderVelocityLeft(){
+ return wheelLeft.velocity(encoderLeft.getPulses());
+ }
+int encoderVelocityRight(){
+ return wheelRight.velocity(encoderRight.getPulses());
+ }
\ No newline at end of file
--- a/encoderBase.h Wed Feb 21 11:55:29 2018 +0000 +++ b/encoderBase.h Thu Feb 22 17:01:00 2018 +0000 @@ -0,0 +1,2 @@ +extern int encoderVelocityLeft(); +extern int encoderVelocityRight(); \ No newline at end of file
--- a/main.cpp Wed Feb 21 11:55:29 2018 +0000
+++ b/main.cpp Thu Feb 22 17:01:00 2018 +0000
@@ -1,20 +1,9 @@
#include "mbed.h"
#include "pwm.h"
+#include "control.h"
+
int main(){
-
motorSetup();
-while(1){
- if(joystickCenter == 1){
- wait(0.5);
- buggyGoF();
- wait(2);
- buggyGoLeft();
- wait(2);
- buggyGoRight();
- wait(2);
- buggyStop();
+ controlManual(0);
}
-}
-
-}
\ No newline at end of file
--- a/pins.cpp Wed Feb 21 11:55:29 2018 +0000 +++ b/pins.cpp Thu Feb 22 17:01:00 2018 +0000 @@ -4,6 +4,7 @@ #include "pins.h" #include "QEI.h" #include "constants.cpp" +#include "C12832.h" //Encoder pins (created as a QEI object, so just defined naming for pins) //======================= #define channelARight PB_1 @@ -16,20 +17,21 @@ QEI encoderLeft(channelALeft, channelBLeft, channelILeft, ENCODER_PULSE); //Motor control pins //======================= -PwmOut motorRight(PB_6); -PwmOut motorLeft(PB_7); -DigitalOut motorDirRight(PB_4); +PwmOut motorRight(PB_7); +PwmOut motorLeft(PC_8); +DigitalOut motorDirRight(PH_0); DigitalOut motorDirLeft(PB_5); -DigitalOut motorModeRight(PB_2); -DigitalOut motorModeLeft(PB_3); -DigitalOut driveBoard (PB_1); +DigitalOut motorModeRight(PC_15); +DigitalOut motorModeLeft(PB_4); +DigitalOut driveBoard (PB_2); //Line sensor pins //======================= //UI pins(setup for control) //======================= -AnalogIn potLeft(PA_0); -AnalogIn potRight(PA_1); -DigitalIn joystickCentre(PB_5); +C12832 lcd(D11, D13, D12, D7, D10); +AnalogIn potentiometerLeft(PA_1); +AnalogIn potentiometerRight(PA_0); +DigitalIn joystickCentre(PB_10); DigitalIn joystickDown(PB_0); //Magnet sensor pins //=======================
--- a/pins.h Wed Feb 21 11:55:29 2018 +0000 +++ b/pins.h Thu Feb 22 17:01:00 2018 +0000 @@ -1,4 +1,5 @@ #include "QEI.h" +#include "C12832.h" extern PwmOut motorRight; extern PwmOut motorLeft; extern DigitalOut motorDirRight; @@ -7,9 +8,10 @@ extern DigitalOut motorModeLeft; extern DigitalOut driveBoard; extern QEI encoderRight; -extern AnalogIn potLeft; -extern AnalogIn potRight; +extern AnalogIn potentiometerLeft; +extern AnalogIn potentiometerRight; extern DigitalIn joystickCentre; extern DigitalIn joystickDown; extern QEI encoderLeft; -extern int ENCODER_PULSE; \ No newline at end of file +extern int ENCODER_PULSE; +extern C12832 lcd; \ No newline at end of file
--- a/pwm.cpp Wed Feb 21 11:55:29 2018 +0000
+++ b/pwm.cpp Thu Feb 22 17:01:00 2018 +0000
@@ -20,46 +20,10 @@
driveBoard.write(1); //enables the buggy to drive
}
-if(joystickDown == 1){
-void buggyGoF(){ //drives forward
- motorRight.write(NORMAL_PWM*potRight.read());
- motorLeft.write(NORMAL_PWM*potLeft.read());
-}
-void buggyGoLeft(){// drives left
- motorRight.write(FAST_PWM*potRight.read());
- motorLeft.write(SLOW_PWM*potLeft.read());
-}
-void buggyGoRight(){ //drives right
- motorRight.write(SLOW_PWM*potRight.read());
- motorLeft.write(FAST_PWM*potLeft.read());
-}
-}
-
-else{
- void buggyGoF(){ //drives forward
- motorRight.write(NORMAL_PWM);
- motorLeft.write(NORMAL_PWM);
-}
-
-void buggyGoLeft(){// drives left
- motorRight.write(FAST_PWM);
- motorLeft.write(SLOW_PWM);
-}
-void buggyGoRight(){ //drives right
- motorRight.write(SLOW_PWM);
- motorLeft.write(FAST_PWM);
-}
+void buggyPWM(float pwmLeft, float pwmRight){ //drives forward
+ motorRight.write(pwmRight);
+ motorLeft.write(pwmLeft);
}
-void buggyStop(){// no power
- motorRight.write(0.0f);
- motorLeft.write(0.0f);
-}
-
-
-void buggyStop(){// no power
- motorRight.write(0.0f);
- motorLeft.write(0.0f);
-}
\ No newline at end of file
--- a/pwm.h Wed Feb 21 11:55:29 2018 +0000 +++ b/pwm.h Thu Feb 22 17:01:00 2018 +0000 @@ -1,5 +1,2 @@ extern void motorSetup(); -extern void buggyGoF(); -extern void buggyGoLeft(); -extern void buggyGoRight(); -extern void buggyStop(); \ No newline at end of file +extern void buggyPWM(float, float); \ No newline at end of file
