Absender: Timo Sandmann
Datum: Fr, 30.03.2007 21:42:25
In-reply-to:
<460D351F.5080107@xxxxxx>
References:
<460BFC4D.5030809@xxxxxx> <03FA8E8E-224D-4E27-A1A6-2899A0C20B7B@xxxxxxxxxxxxxxx> <460C243C.907@xxxxxx> <FB69CB24-398E-4A0D-A099-3E8751094F34@xxxxxxxxxxxxxxx> <460CC23C.8090408@xxxxxx> <460CD36D.50200@xxxxxxxx> <460CDD44.3050409@xxxxxx> <460CF0A1.404@xxxxxx> <EEA5B6A0-CA7B-479F-BF39-F194D98CBDB2@xxxxxxxxxxxxxxx> <460D351F.5080107@xxxxxx>
Hallo,hier ein Patch gegen das CVS, der das Problem lösen sollte. Ich hatte jetzt allerdings noch keine Zeit sämtliche Funktionen komplett zu testen. Damit die EEPROM-Entwicklung aber nicht darauf warten muss, hier vorab die Korrekturen. (u)intX sind jetzt wirklich immer X Bit groß. Für den Timer gibt es neben dem Bugfix des PC-Codes nun noch eine Funktion zur einfachen Wartezeitberechnung, siehe Dokumentation von timer_ms_passed() in timer.h
Gruß Timo --------------------------------------------- ### Eclipse Workspace Patch 1.0 #P ct-Bot Index: bot-logic/behaviour_servo.c =================================================================== RCS file: /ctbot/ct-Bot/bot-logic/behaviour_servo.c,v retrieving revision 1.1 diff -u -r1.1 behaviour_servo.c --- bot-logic/behaviour_servo.c 15 Jan 2007 11:07:33 -0000 1.1 +++ bot-logic/behaviour_servo.c 30 Mar 2007 19:54:26 -0000 @@ -43,8 +43,8 @@ * @param *data der Verhaltensdatensatz */ void bot_servo_behaviour(Behaviour_t *data){ - /* Servo ausschalten, falls Klappe zu oder Countdown abgelaufen */- if ( (servo_pos == DOOR_CLOSE && sensDoor == 0) || (TIMER_GET_TICKCOUNT_16 - servo_time > MS_TO_TICKS(1000L)) ){
+ /* Servo ausschalten, falls Countdown abgelaufen */ + if (timer_ms_passed(&servo_time, 1000)) { servo_set(servo_nr, SERVO_OFF); // Servo aus servo_active &= ~servo_nr; return_from_behaviour(data); // und Verhalten auch aus @@ -57,6 +57,7 @@ * @param pos Zielposition des Servos */ void bot_servo(Behaviour_t * caller, uint8 servo, uint8 pos){+ if (pos == DOOR_CLOSE && sensDoor == 0) return; // Klappe ist bereits geschlossen
servo_active |= servo; servo_set(servo, pos); // Servo-PWM einstellen servo_pos = pos; // Zielposition merken Index: timer.c =================================================================== RCS file: /ctbot/ct-Bot/timer.c,v retrieving revision 1.6 diff -u -r1.6 timer.c --- timer.c 15 Jan 2007 11:08:37 -0000 1.6 +++ timer.c 30 Mar 2007 19:54:26 -0000 @@ -107,8 +107,9 @@ LOCK(); /* TickCounter [176 us] erhoehen */ static uint16 last_simultime=0;- if (simultime < last_simultime) last_simultime -= 10000; // der Sim setzt simultime alle 10s zurueck auf 0
- tickCount += MS_TO_TICKS((float)(simultime - last_simultime)); + float tmp = (float)simultime - (float)last_simultime;+ if (tmp < 0) tmp += 10000; // der Sim setzt simultime alle 10s zurueck auf 0
+ tickCount += MS_TO_TICKS(tmp); last_simultime = simultime; UNLOCK(); } Index: command.c =================================================================== RCS file: /ctbot/ct-Bot/command.c,v retrieving revision 1.23 diff -u -r1.23 command.c --- command.c 5 Mar 2007 12:06:25 -0000 1.23 +++ command.c 30 Mar 2007 19:54:26 -0000 @@ -145,8 +145,8 @@// So lange Daten lesen, bis das Packet vollstaendig ist, oder der Timeout zuschlaegt
while (i > 0){ // Wenn der Timeout ueberschritten ist- if (TIMER_GET_TICKCOUNT_16-old_ticks > MS_TO_TICKS (COMMAND_TIMEOUT)){
- LOG_DEBUG(("Timeout beim nachlesen")); + if (timer_ms_passed(&old_ticks, COMMAND_TIMEOUT)) { + LOG_DEBUG("Timeout beim nachlesen"); return -1; // ==> Abbruch } // LOG_DEBUG(("%d bytes missing",i)); Index: sensor.c =================================================================== RCS file: /ctbot/ct-Bot/sensor.c,v retrieving revision 1.29 diff -u -r1.29 sensor.c --- sensor.c 22 Feb 2007 18:02:10 -0000 1.29 +++ sensor.c 30 Mar 2007 19:54:26 -0000 @@ -166,9 +166,7 @@ sensMouseX += sensMouseDX; /*!< Mausdelta X aufaddieren */ #endif - register uint16 ticks = TIMER_GET_TICKCOUNT_16; - if (ticks-old_pos > MS_TO_TICKS(50)){ - old_pos = ticks; + if (timer_ms_passed(&old_pos, 50)) { /* Gefahrene Boegen aus Encodern berechnen */ diffEncL=sensEncL-lastEncL; diffEncR=sensEncR-lastEncR; @@ -243,9 +241,7 @@ #endif #endif } - ticks = TIMER_GET_TICKCOUNT_16; - if (ticks-old_speed > MS_TO_TICKS(250)){ - old_speed=ticks; + if (timer_ms_passed(&old_speed, 250)) {v_enc_left= (((sensEncL - lastEncL1) * WHEEL_PERIMETER) / ENCODER_MARKS)*4; v_enc_right= (((sensEncR - lastEncR1) * WHEEL_PERIMETER) / ENCODER_MARKS)*4;
v_enc_center=(v_enc_left+v_enc_right)/2; Index: motor.c =================================================================== RCS file: /ctbot/ct-Bot/motor.c,v retrieving revision 1.14 diff -u -r1.14 motor.c --- motor.c 15 Jan 2007 11:08:37 -0000 1.14 +++ motor.c 30 Mar 2007 19:54:26 -0000 @@ -196,7 +196,7 @@ // Hier sitzt die eigentliche Regelung #ifdef SPEED_CONTROL_AVAILABLE register uint16 ticks = TIMER_GET_TICKCOUNT_16;- if (ticks-old_mot_ticks > MS_TO_TICKS((uint16) SPEED_CONTROL_INTERVAL)) { + if ((uint16)(ticks-old_mot_ticks) > MS_TO_TICKS((uint16) SPEED_CONTROL_INTERVAL)) {
speed_control(); old_mot_ticks = TIMER_GET_TICKCOUNT_16; } Index: include/global.h =================================================================== RCS file: /ctbot/ct-Bot/include/global.h,v retrieving revision 1.6 diff -u -r1.6 global.h --- include/global.h 8 Jun 2006 16:36:38 -0000 1.6 +++ include/global.h 30 Mar 2007 19:54:26 -0000 @@ -1,5 +1,5 @@ /* - * c't-Sim - Robotersimulator fuer den c't-Bot + * c't-Bot * * This program is free software; you can redistribute it * and/or modify it under the terms of the GNU General @@ -17,14 +17,17 @@ * */ -/*! @file global.h +/*! + * @file global.h * @brief Allgemeine Definitionen * @author Benjamin Benz (bbe@xxxxxxxx) * @date 20.12.05 -*/ + */ #ifndef global_H - #define global_H /*!< Bereits definiert */ + #define global_H + #ifndef __ASSEMBLER__ + #include <stdint.h> #ifndef MCU #ifndef PC @@ -33,8 +36,9 @@ #endif #ifndef WIN32- typedef unsigned char byte; /*!< vorzeichenlose 8-Bit-Zahl */ - typedef byte bool; /*!< True/False- Aussage */
+// typedef unsigned char byte; /*!< vorzeichenlose 8-Bit-Zahl */ +// typedef byte bool; /*!< True/False-Aussage */ + #define bool byte; /*!< True/False-Aussage */ #endif //#define DOXYGEN /*!< Nur zum Generieren von Doku!!!! */ @@ -45,23 +49,33 @@ #define __linux__ /*!< System Linux */ #endif- typedef unsigned char uint8; /*!< vorzeichenlose 8-Bit-Zahl */ - typedef unsigned int word; /*!< vorzeichenlose 16-Bit-Zahl */ - typedef signed char int8; /*!< vorzeichenbehaftete 8-Bit-Zahl */ - typedef short int int16; /*!< vorzeichenbehaftete 16-Bit-Zahl */
- - typedef unsigned long uint32; /*!< vorzeichenlose 32-Bit-Zahl */ - typedef signed long int32; /*!< vorzeichenbehaftete 32-Bit-Zahl */ - - #define uint16 word /*!< Int mit 16 Bit */ +// deprecated!+// typedef unsigned char uint8; /*!< vorzeichenlose 8-Bit-Zahl */ +// typedef unsigned int word; /*!< vorzeichenlose 16-Bit-Zahl */ +// typedef signed char int8; /*!< vorzeichenbehaftete 8-Bit-Zahl */ +// typedef short int int16; /*!< vorzeichenbehaftete 16-Bit-Zahl */
+// +// typedef unsigned long uint32; /*!< vorzeichenlose 32-Bit-Zahl */+// typedef signed long int32; /*!< vorzeichenbehaftete 32-Bit- Zahl */
+// +// #define uint16 word /*!< Int mit 16 Bit */ + + #define byte uint8_t /*!< vorzeichenlose 8-Bit-Zahl */ + #define uint8 uint8_t /*!< vorzeichenlose 8-Bit-Zahl */ + #define int8 int8_t /*!< vorzeichenbehaftete 8-Bit-Zahl */ + #define uint16 uint16_t /*!< vorzeichenlose 16-Bit-Zahl */ + #define int16 int16_t /*!< vorzeichenbehaftete 16-Bit-Zahl */ + #define uint32 uint32_t /*!< vorzeichenlose 32-Bit-Zahl */ + #define int32 int32_t /*!< vorzeichenbehaftete 32-Bit-Zahl */ - #define True 1 /*!< Wahr */ - #define False 0 /*!< Falsch */ + #define True 1 /*!< Wahr */ + #define False 0 /*!< Falsch */ - #define On 1 /*!< An */ - #define Off 0 /*!< Aus */ + #define On 1 /*!< An */ + #define Off 0 /*!< Aus */-// #define PI 3.14159 /*!< Kreiszahl Pi fuer trigonometrische Berechnungen */ +// #define PI 3.14159 /*!< Kreiszahl Pi fuer trigonometrische Berechnungen */
#define binary(var,bit) ((var >> bit)&1) //#define NULL 0 -#endif +#endif // __ASSEMBLER__ +#endif // global_H Index: include/timer.h =================================================================== RCS file: /ctbot/ct-Bot/include/timer.h,v retrieving revision 1.12 diff -u -r1.12 timer.h --- include/timer.h 15 Jan 2007 11:07:33 -0000 1.12 +++ include/timer.h 30 Mar 2007 19:54:26 -0000 @@ -1,5 +1,5 @@ /* - * c't-Sim - Robotersimulator fuer den c't-Bot + * c't-Bot * * This program is free software; you can redistribute it * and/or modify it under the terms of the GNU General @@ -17,16 +17,18 @@ * */ -/*! @file timer.h +/*! + * @file timer.h * @brief Timer und Zaehler * @author Benjamin Benz (bbe@xxxxxxxx) * @date 26.12.05 -*/ + */ #ifndef TIMER_H_ #define TIMER_H_ #include "ct-Bot.h" +#include <stdint.h> /*! * Makros zur Umrechnung von Ticks in ms und zurueck @@ -94,6 +96,52 @@ */ // #define TIMER_STEPS (1 000 000/TIMER_2_CLOCK) #define TIMER_STEPS 176 + +/*!+ * @brief Prueft, ob seit dem letzten Aufruf mindestens ms Millisekunden vergangen sind + * @param old_ticks Zeiger auf eine Variable, die einen Timestamp speichern kann + * @param ms Zeit in ms, die vergangen sein muss, damit True geliefert wird
+ * @return True oder False + *+ * Die Funktion aktualisiert den Timestamp, der die alte Zeit zum Vergleich speichert, automatisch,
+ * falls ms Millisekunden vergangen sind. + * Man verwendet sie z.B. wie folgt: + * static uint32 old_time; + * ... + * if (timer_ms_passed(&old_time, 50)) { + * // wird alle 50 ms ausgefuehrt // + * } + */+static inline uint8_t __attribute__((always_inline)) timer_ms_passed (void* old_ticks, uint32_t ms) {
+ + /* 8 Bit Version */ + if (MS_TO_TICKS(ms) < UINT8_MAX) { + register uint8_t ticks = TIMER_GET_TICKCOUNT_8; + if ((uint8_t)(ticks - *(uint8*)old_ticks) > MS_TO_TICKS(ms)) { + *(uint8_t*)old_ticks = ticks; + return True; + } + return False; + + /* 16 Bit Version */ + } else if (MS_TO_TICKS(ms) < UINT16_MAX) { + register uint16_t ticks = TIMER_GET_TICKCOUNT_16; + if ((uint16_t)(ticks - *(uint16_t*)old_ticks) > MS_TO_TICKS(ms)) { + *(uint16_t*)old_ticks = ticks; + return True; + } + return False; + + /* 32 Bit Version */ + } else { + register uint32_t ticks = TIMER_GET_TICKCOUNT_32; + if ((uint32_t)(ticks - *(uint32_t*)old_ticks) > MS_TO_TICKS(ms)) { + *(uint32_t*)old_ticks = ticks; + return True; + } + return False; + } +} /*! * Initialisiert Timer 2 und startet ihn Index: mcu/sensor-low.c =================================================================== RCS file: /ctbot/ct-Bot/mcu/sensor-low.c,v retrieving revision 1.12 diff -u -r1.12 sensor-low.c --- mcu/sensor-low.c 15 Jan 2007 11:07:33 -0000 1.12 +++ mcu/sensor-low.c 30 Mar 2007 19:54:26 -0000 @@ -148,7 +148,7 @@ static int16 distRight[3]; register uint16 dist_ticks = TIMER_GET_TICKCOUNT_16; - if (dist_ticks-old_dist > MS_TO_TICKS(100)){ + if ((uint16)(dist_ticks-old_dist) > MS_TO_TICKS(100)){ // Zeit fuer naechste Messung merken old_dist=dist_ticks;