added accel and brake parameters to car firmware

Fri, 27 Dec 2013 11:43:40 +0100

author
mbayer
date
Fri, 27 Dec 2013 11:43:40 +0100
changeset 153
80d869ac365b
parent 152
e787f47c084b
child 154
46822de910ff

added accel and brake parameters to car firmware

blackbox/main.h file | annotate | diff | comparison | revisions
car004f/main.c file | annotate | diff | comparison | revisions
--- a/blackbox/main.h	Mon Dec 23 14:05:53 2013 +0100
+++ b/blackbox/main.h	Fri Dec 27 11:43:40 2013 +0100
@@ -83,6 +83,7 @@
 #define VALUE2_250US    2.5e-4
 #define TIMER2_250US    (uint8_t)((VALUE2_250US * F_CPU/TIMER2_DIVISOR) - 2)
 
+
 #define TIMER1_DIVISOR  8
 #define VALUE1_7500NS   0.75e-2
 #define TIMER1_7500NS   (uint16_t)(VALUE1_7500NS * F_CPU/TIMER1_DIVISOR)
--- a/car004f/main.c	Mon Dec 23 14:05:53 2013 +0100
+++ b/car004f/main.c	Fri Dec 27 11:43:40 2013 +0100
@@ -21,9 +21,11 @@
     uint8_t initialized; // if 0xff, reset config to defaults on first boot
     uint8_t slot;
     uint8_t light;
+    uint8_t brake;
+    uint8_t accel;
     uint8_t program; // 0xff = inactive ; programming mode active on slot X
 } config_t;
-config_t EEMEM eeconfig = {0,0,0,0xff,0};
+config_t EEMEM eeconfig = {0,0xff,0,0,15,15,0};
 config_t config;
 
 volatile uint16_t data = 0;
@@ -32,6 +34,7 @@
 volatile uint16_t bitbuf = 0;
 volatile uint8_t car_speed[MAX_SLOTS];
 volatile uint8_t car_switch[MAX_SLOTS];
+volatile uint8_t car_act[MAX_SLOTS];
 
 volatile uint8_t car_timeout[MAX_SLOTS];
 volatile uint8_t timeout = 0;
@@ -50,11 +53,13 @@
     bitbuf = 0; // init
     bitbuf_len = 0b10000000; // init 1 pulse received
 
-    TCNT2 = 10;
+    //TCNT2 = 10;
+    TCNT2 = 9;
     TIMSK |= _BV(OCIE2); //enable timer2 interrupt
 }
 
 ISR ( TIMER2_COMP_vect ) {
+//    TCNT2 = 0;
     uint8_t clock;
     uint8_t state;
     uint8_t state2;
@@ -89,10 +94,6 @@
                 */
             }
 
-            // bugfix 20131223: timing errors, CLEAR timer2 flag before enable
-            // if this works, apply to track switches and pitlane too!
-            //GIFR = _BV(INTF0);
-
             GICR |= _BV(INT0) ; // Enable INT0
 
         } else {
@@ -142,12 +143,14 @@
 
 
 void brake_on(void) {
+    OCR1A = (int) ((float)0xff * (float)((float)config.brake / (float)15));
     LIGHT_PORT |= _BV(LIGHT_BRAKE); // brake light on
     DDRB |= _BV(1);                  // PB1 PWM Output enable
     brake_timeout = BRAKE_OFF_TIMEOUT;
 }
 
 void brake_off(void) {
+    OCR1A = 0;
     LIGHT_PORT &= ~_BV(LIGHT_BRAKE); // brake light off
     DDRB &= ~_BV(1);                  // PB1 PWM Output disable
     brake_timeout = 0;
@@ -204,6 +207,8 @@
     if (config.initialized == 0xff) {
         config.slot = 0;
         config.light = 0;
+        config.brake = 15;
+        config.accel = 15;
         config.program = 0xff;
         config.initialized = 0;
         config_save();
@@ -233,7 +238,7 @@
 
     //OCR1A = 63;                       // Dutycycle of OC1A = 25%
     //OCR1B = 127;                      // Dutycycle of OC1B = 50%
-    OCR1A = 0xff; // brake PWM!
+    OCR1A = 0; // brake PWM!
     OCR1B = 0; // Motor drive PWM
     DDRB &= ~_BV(2);                  // PB2 PWM Output disable
     DDRB &= ~_BV(1);                  // PB1 PWM Output disable
@@ -266,8 +271,10 @@
     }
 
 
+    float my_accel;
     my_switch = car_switch[config.slot]; // initialize
     my_speed = car_speed[config.slot]; // initialize
+    my_accel =  (float)config.accel / (float)15;
 
     while (1) {
         // main loop
@@ -276,7 +283,7 @@
 
         if (my_speed != car_speed[config.slot]) {
             my_speed = car_speed[config.slot];
-            OCR1B = (int) ((float)0xff * (float)((float)my_speed / (float)15));
+            OCR1B = (int) ((float)0xff * (float)((float)my_speed / (float)15) * my_accel );
             if (my_speed == 0) {
                 DDRB &= ~_BV(2);                  // PB2 PWM Output disable
                 brake_on();

mercurial