add cksum
authorOlivier MATZ <zer0@droids-corp.org>
Sun, 7 Mar 2010 21:15:25 +0000 (22:15 +0100)
committerOlivier MATZ <zer0@droids-corp.org>
Sun, 7 Mar 2010 21:15:25 +0000 (22:15 +0100)
projects/microb2010/tests/beacon_tsop/main.c
projects/microb2010/tests/static_beacon/.config
projects/microb2010/tests/static_beacon/static_beacon.c

index 939719f..edc444b 100755 (executable)
@@ -146,6 +146,31 @@ void debug_tsop(void)
 #endif
 }
 
+#if 0
+/* val is 16 bits, including 4 bits-cksum in MSB, return 0xFFFF is
+ * cksum is wrong, or the 12 bits value on success. */
+static uint16_t verify_cksum(uint16_t val)
+{
+       uint16_t x, cksum;
+
+       x = (val & 0xfff);
+       /* add the four 4-bits blocks of val together */
+       cksum = val & 0xf;
+       val = val >> 4;
+       cksum += val & 0xf;
+       cksum = (cksum & 0xf) + ((cksum & 0xf0) >> 4);
+       val = val >> 4;
+       cksum += val & 0xf;
+       cksum = (cksum & 0xf) + ((cksum & 0xf0) >> 4);
+       val = val >> 4;
+       cksum += val & 0xf;
+       cksum = (cksum & 0xf) + ((cksum & 0xf0) >> 4);
+       if (cksum == 0xf)
+               return x;
+       return 0xffff; /* wrong cksum */
+}
+#endif
+
 /* decode frame */
 SIGNAL(SIG_TSOP) {
        static uint8_t led_cpt = 0;
index 5c126d5..8c61239 100644 (file)
@@ -1,5 +1,5 @@
 #
-# Automatically generated make config: don't edit
+# Automatically generated by make menuconfig: don't edit
 #
 
 #
@@ -61,11 +61,12 @@ CONFIG_QUARTZ=16000000
 # CONFIG_OPTM_2 is not set
 # CONFIG_OPTM_3 is not set
 CONFIG_OPTM_S=y
-# CONFIG_MATH_LIB is not set
+CONFIG_MATH_LIB=y
 # CONFIG_FDEVOPEN_COMPAT is not set
 # CONFIG_NO_PRINTF is not set
 # CONFIG_MINIMAL_PRINTF is not set
 CONFIG_STANDARD_PRINTF=y
+# CONFIG_ADVANCED_PRINTF is not set
 # CONFIG_FORMAT_IHEX is not set
 # CONFIG_FORMAT_SREC is not set
 CONFIG_FORMAT_BINARY=y
@@ -73,10 +74,6 @@ CONFIG_FORMAT_BINARY=y
 #
 # Base modules
 #
-
-#
-# Enable math library in generation options to see all modules
-#
 CONFIG_MODULE_CIRBUF=y
 # CONFIG_MODULE_CIRBUF_LARGE is not set
 # CONFIG_MODULE_FIXED_POINT is not set
@@ -97,10 +94,6 @@ CONFIG_MODULE_SCHEDULER_TIMER0=y
 #
 # Communication modules
 #
-
-#
-# uart needs circular buffer, mf2 client may need scheduler
-#
 CONFIG_MODULE_UART=y
 # CONFIG_MODULE_UART_9BITS is not set
 CONFIG_MODULE_UART_CREATE_CONFIG=y
@@ -185,10 +178,6 @@ CONFIG_MODULE_UART_CREATE_CONFIG=y
 # Control system modules
 #
 # CONFIG_MODULE_CONTROL_SYSTEM_MANAGER is not set
-
-#
-# Filters
-#
 # CONFIG_MODULE_PID is not set
 # CONFIG_MODULE_PID_CREATE_CONFIG is not set
 # CONFIG_MODULE_RAMP is not set
@@ -199,20 +188,12 @@ CONFIG_MODULE_UART_CREATE_CONFIG=y
 #
 # Radio devices
 #
-
-#
-# Some radio devices require SPI to be activated
-#
 # CONFIG_MODULE_CC2420 is not set
 # CONFIG_MODULE_CC2420_CREATE_CONFIG is not set
 
 #
 # Crypto modules
 #
-
-#
-# Crypto modules depend on utils module
-#
 # CONFIG_MODULE_AES is not set
 # CONFIG_MODULE_AES_CTR is not set
 # CONFIG_MODULE_MD5 is not set
@@ -222,20 +203,12 @@ CONFIG_MODULE_UART_CREATE_CONFIG=y
 #
 # Encodings modules
 #
-
-#
-# Encoding modules depend on utils module
-#
 # CONFIG_MODULE_BASE64 is not set
 # CONFIG_MODULE_HAMMING is not set
 
 #
 # Debug modules
 #
-
-#
-# Debug modules depend on utils module
-#
 # CONFIG_MODULE_DIAGNOSTIC is not set
 # CONFIG_MODULE_DIAGNOSTIC_CREATE_CONFIG is not set
 # CONFIG_MODULE_ERROR is not set
index 08b4251..9f9c8d6 100755 (executable)
@@ -20,6 +20,8 @@
  *
  */
 
+#include <math.h>
+
 #include <aversive.h>
 #include <aversive/wait.h>
 
@@ -50,7 +52,7 @@
 #define BEACON_ID_SHIFT 0
 
 /* */
-#define FRAME_DATA_MASK  0xFFF8
+#define FRAME_DATA_MASK  0x0FF8
 #define FRAME_DATA_SHIFT 3
 
 #if (defined MODUL_455KHZ)
 #error "no freq defined"
 #endif
 
+#define MIN_DIST 150.
+#define MAX_DIST 3600.
+#define LASER_DIST 25.
+
 #define N_CYCLES_PERIOD (N_CYCLES_0 + N_CYCLES_1)
 
 #define LED_PORT PORTD
@@ -186,13 +192,62 @@ static void xmit_bits(uint32_t frame, uint8_t nbit)
 }
 
 #ifdef WAIT_LASER
-/* get the frame from laser time difference */
+/* val is 12 bits. Return the 16 bits value that includes the 4 bits
+ * cksum in MSB. */
+static uint16_t do_cksum(uint16_t val)
+{
+       uint16_t x, cksum;
+
+       x = (val & 0xfff);
+       /* add the three 4-bits blocks of x together */
+       cksum = x & 0xf;
+       x = x >> 4;
+       cksum += x & 0xf;
+       cksum = (cksum & 0xf) + ((cksum & 0xf0) >> 4);
+       x = x >> 4;
+       cksum += x & 0xf;
+       cksum = (cksum & 0xf) + ((cksum & 0xf0) >> 4);
+       cksum = (~cksum) & 0xf;
+       return (cksum << 12) + (val & 0xfff);
+}
+
+/* get the frame from laser time difference, return 0 on error (this
+ * frame cannot be sent). */
 static uint32_t get_frame(uint16_t laserdiff)
 {
        uint32_t frame = 0;
+       double a, d;
+       uint16_t frame_dist;
+
        frame |= ((uint32_t)BEACON_ID << BEACON_ID_SHIFT);
-       frame |= ((uint32_t)(laserdiff & FRAME_DATA_MASK) << FRAME_DATA_SHIFT);
-       return frame;
+
+       /* process angle from laserdiff time */
+#ifdef SPEED_10RPS
+       /* timer = 2Mhz */
+       a = ((double)laserdiff / (2000000./10.)) * 2. * M_PI;
+#else
+       /* timer = 16Mhz */
+       a = ((double)laserdiff / (16000000./40.)) * 2. * M_PI;
+#endif
+       /* get distance from angle */
+       d = LASER_DIST / sin(a/2);
+
+       /* scale it between 0 and 511 */
+       if (d <= MIN_DIST)
+               return 0;
+       if (d >= MAX_DIST)
+               return 0;
+       d -= MIN_DIST;
+       d /= (MAX_DIST-MIN_DIST);
+       d *= 512;
+       frame_dist = (uint16_t)d;
+       if (frame_dist >= 512) /* should not happen... */
+               return 0;
+
+       frame |= ((uint32_t)(frame_dist & FRAME_DATA_MASK) << FRAME_DATA_SHIFT);
+
+       /* process cksum and return */
+       return do_cksum(frame);
 }
 
 /* Wait 2 consecutive rising edges on photodiode. Return 0 on success,
@@ -330,6 +385,11 @@ int main(void)
                        continue;
 
                frame = get_frame(diff);
+               /* cannot convert into frame... skip it */
+               if (frame == 0) {
+                       wait_ms(INTER_LASER_TIME);
+                       continue;
+               }
 
                /* wait before IR xmit */
                while ((int16_t)(when-TCNT1) > 0);