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: FRDM-TFC mbed CBuffer XBEE mbed_angular_speed motor2 MMA8451Q
Fork of KL25Z_Camera_Test by
Revision 45:3435bdd2d487, committed 2017-03-23
- Comitter:
- FatCookies
- Date:
- Thu Mar 23 09:36:39 2017 +0000
- Parent:
- 44:1884ffec9a57
- Child:
- 46:6806ea59ffed
- Commit message:
- testing out accelerometer data for slope detection
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MMA8451Q.lib Thu Mar 23 09:36:39 2017 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/emilmont/code/MMA8451Q/#c4d879a39775
--- a/main.cpp Mon Mar 20 12:23:34 2017 +0000
+++ b/main.cpp Thu Mar 23 09:36:39 2017 +0000
@@ -15,6 +15,7 @@
#include "angular_speed.h"
#include "main.h"
#include "motor.h"
+#include "MMA8451Q.h"
// Serial
#if USE_COMMS
@@ -34,8 +35,15 @@
//testing timer for laptimes
Timer lapTimer;
+MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
+
+
+
+int loop = 0;
+float accc = 0.f;
int main() {
+
//Set up TFC driver stuff
TFC_Init();
ALIGN_SERVO;
@@ -52,6 +60,7 @@
#if !(USE_COMMS)
buttonStartup();
#endif
+
while(1) {
@@ -66,7 +75,19 @@
servo_pid.measured_value = findCentreValue(CLOSE_CAMERA, left, right);
// Check if car is at the stop line
- handleStartStop();
+
+ if(loop % 10 == 0) {
+ accc = checkAcc();
+ }
+
+ if(accc < 0.18f) {
+ handleStartStop();
+ } else {
+ sendString("up %f", accc);
+ }
+
+
+
#if USE_COMMS
// Send the line scan image over serial
@@ -93,15 +114,17 @@
-
+ //wait_ms(25);
}
}
}
void buttonStartup() {
+ TFC_SetBatteryLED(led_values[b_pressed % 4]);
while(1) {
-
+ //handleComms();
+
// 2 bit is broken = max value is 13
uint8_t dip = TFC_GetDIP_Switch();
@@ -113,58 +136,73 @@
float pot_a = TFC_ReadPot(0);
float pot_b = TFC_ReadPot(1);
- if(!aDown && button_a) {
- aDown = true;
- } else if(aDown && !button_a) {
+ if(button_b && !bDown) {
+ bDown = true;
+ continue;
+ }
+ if(!button_b && bDown) {
+ b_pressed++;
+ TFC_SetBatteryLED(led_values[b_pressed % 4]);
+ bDown = false;
+ continue;
+ }
+
+ if(button_a && !aDown) {
+ aDown = true;
+ continue;
+ }
+ if(!button_a && aDown) {
+
+ TFC_SetBatteryLED(0);
aDown = false;
int choice = b_pressed % 4;
switch(choice) {
- case 0:
+ case 0:
initPID(&servo_pid, 2.2f, 0.6f, 0.f);
- speed = 100;
+ speed = 40;
+ ed_tune = 1.0f;
break;
case 1:
initPID(&servo_pid, 2.2f, 0.6f, 0.f);
speed = 120;
+ ed_tune = 1.0f;
break;
case 2:
initPID(&servo_pid, 2.2f, 0.6f, 0.f);
speed = 130;
+ ed_tune = 1.0f;
break;
case 3:
initPID(&servo_pid, 2.2f, 0.6f, 0.f);
speed = 140;
+ ed_tune = 1.0f;
break;
- }
- wait(1.f);
+ }
+
+ wait(2.f);
- ALIGN_SERVO;
- right_motor_pid.desired_value=speed;
- left_motor_pid.desired_value=speed;
- TFC_HBRIDGE_ENABLE;
-
-
- servo_pid.integral = 0;
- return;
+ ALIGN_SERVO;
+ right_motor_pid.desired_value=speed;
+ left_motor_pid.desired_value=speed;
+ TFC_HBRIDGE_ENABLE;
+ servo_pid.integral = 0;
+ //sendString("PID: %f %f %f speed:%f ed:%f",servo_pid.Kp, servo_pid.Ki, servo_pid.Kd, speed, ed_tune);
+ return;
}
- if(!bDown && button_b) {
- aDown = true;
- } else if(bDown && !button_b) {
- aDown = false;
- TFC_SetBatteryLED(led_values[b_pressed % 4]);
- b_pressed++;
- }
+
}
+
+}
-}
+
void initVariables() {
// Initialise three PID controllers for the servo and each wheel.
initPID(&servo_pid, 0.f, 0.f, 0.f);
- initPID(&left_motor_pid, 0.01f, 0.f, 0.f);
- initPID(&right_motor_pid, 0.01f, 0.f, 0.f);
+ initPID(&left_motor_pid, 0.007f, 0.f, 0.f);
+ initPID(&right_motor_pid, 0.007f, 0.f, 0.f);
right_motor_pid.desired_value=0;
left_motor_pid.desired_value=0;
@@ -413,7 +451,6 @@
TFC_SetMotorPWM(left_motor_pid.output,right_motor_pid.output);
-
t.stop();
t.reset();
t.start();
@@ -440,15 +477,15 @@
lastPixel = currentPixel;
}
//Was used to send an indication that the marker was seen, useful for debugging
- //if (slower % 1000 == 0) {
+ //if (slower % 1000 == `0) {
//sendString("Transitions seen: %d", transitionsSeen);
//}
//slower++;
if(transitionsSeen >= 5) {
//Stop the car!
#if USE_COMMS
- sendString("Start/stop seen");
- lapTime();
+ //sendString("Start/stop seen");
+ //lapTime();
#endif
TFC_SetMotorPWM(0.f,0.f);
TFC_HBRIDGE_DISABLE;
@@ -456,6 +493,10 @@
}
}
+float checkAcc() {
+ return abs(acc.getAccY());
+}
+
inline void initSpeedSensors() {
t1.start();
--- a/main.h Mon Mar 20 12:23:34 2017 +0000 +++ b/main.h Thu Mar 23 09:36:39 2017 +0000 @@ -3,7 +3,7 @@ #define CAM_DIFF 10 #define WHEEL_RADIUS 0.025f #define RIGHT_MOTOR_COMPENSATION_RATIO 1.1586276 -#define USE_COMMS 0 +#define USE_COMMS 1 #define CLOSE_CAMERA TFC_LineScanImage0 #define LOOKAHEAD_CAMERA TFC_LineScanImage1 @@ -127,3 +127,21 @@ float oldTime; int lapNo; + +//Accelerometer: +float checkAcc(); + +#if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) + PinName const SDA = PTE25; + PinName const SCL = PTE24; +#elif defined (TARGET_KL05Z) + PinName const SDA = PTB4; + PinName const SCL = PTB3; +#elif defined (TARGET_K20D50M) + PinName const SDA = PTB1; + PinName const SCL = PTB0; +#else + #error TARGET NOT DEFINED +#endif + +#define MMA8451_I2C_ADDRESS (0x1d<<1)
