From: zer0 Date: Mon, 25 Jan 2010 00:07:11 +0000 (+0100) Subject: hostsim enhancements, some bugs remaining (freeze sometimes) X-Git-Url: http://git.droids-corp.org/?p=aversive.git;a=commitdiff_plain;h=a16ba178c7ef15fb408593b0e7c666ed2ed1d253;hp=87ccd3af8abb0da3e0fa98dc8e9216fc7b676f97 hostsim enhancements, some bugs remaining (freeze sometimes) --- diff --git a/include/aversive/irq_lock.h b/include/aversive/irq_lock.h index c8afdc4..ec45b68 100644 --- a/include/aversive/irq_lock.h +++ b/include/aversive/irq_lock.h @@ -42,10 +42,12 @@ #ifdef HOST_VERSION +#include + /* we must use 'flags' to avoid a warning */ -#define IRQ_UNLOCK(flags) flags=0 -#define IRQ_LOCK(flags) flags=0 -#define GLOBAL_IRQ_ARE_MASKED() (1) +#define IRQ_UNLOCK(flags) do { flags=0; hostsim_lock(); } while(0) +#define IRQ_LOCK(flags) do { flags=0; hostsim_unlock(); } while(0) +#define GLOBAL_IRQ_ARE_MASKED() hostsim_islocked() #else diff --git a/modules/base/hostsim/hostsim.c b/modules/base/hostsim/hostsim.c index 87bbfc6..bb77ad9 100644 --- a/modules/base/hostsim/hostsim.c +++ b/modules/base/hostsim/hostsim.c @@ -49,7 +49,7 @@ #include #endif -pthread_mutex_t mut; +static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; static volatile int cpt = 0; static struct termios oldterm; @@ -99,6 +99,26 @@ void sigusr1(__attribute__((unused)) int sig) #endif } +static int lock_count = 0; + +void hostsim_lock(void) +{ + if (lock_count++) + return; + pthread_mutex_lock(&mut); +} + +void hostsim_unlock(void) +{ + if (lock_count-- == 1) + pthread_mutex_unlock(&mut); +} + +int hostsim_islocked(void) +{ + return lock_count; +} + void host_wait_ms(int ms) { struct timeval tv, tv2, diff; @@ -245,8 +265,6 @@ int hostsim_init(void) pthread_t parent_id, child_id, child2_id, child3_id; int ret; - pthread_mutex_init(&mut, NULL); - parent_id = pthread_self(); pthread_mutex_lock(&mut); @@ -291,10 +309,11 @@ int hostsim_init(void) return 0; } -void hostsim_exit(void) +int hostsim_exit(void) { #ifdef CONFIG_MODULE_UART tcsetattr(0, TCSANOW, &oldterm); #endif + return 0; } #endif /* HOST_VERSION */ diff --git a/modules/base/hostsim/hostsim.h b/modules/base/hostsim/hostsim.h index cc41183..176a52e 100644 --- a/modules/base/hostsim/hostsim.h +++ b/modules/base/hostsim/hostsim.h @@ -26,3 +26,7 @@ int hostsim_exit(void); /* replacement for wait_ms() */ void host_wait_ms(int ms); + +void hostsim_lock(void); +void hostsim_unlock(void); +int hostsim_islocked(void); diff --git a/modules/base/time/clock_time.h b/modules/base/time/clock_time.h new file mode 100644 index 0000000..02e8fe6 --- /dev/null +++ b/modules/base/time/clock_time.h @@ -0,0 +1,95 @@ +/* + * Copyright Droids Corporation, Microb Technology, Eirbot (2005) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Revision : $Id: time.h,v 1.3.4.2 2007-05-23 17:18:11 zer0 Exp $ + * + */ + +/* Droids-corp, Eirbot, Microb Technology 2005 - Zer0 + * Interface of the time module + */ + +/** + * This module can be used to get a human readable time. It uses the + * scheduler module. Its goal is not to be very precise, but just + * simple to use. provides two timers: one in s and us, and one in + * us which doesn't overflow on seconds (better to substract two + * times) + */ + +#ifndef _CLOCK_TIME_H_ +#define _CLOCK_TIME_H_ + +#include + +/* a 16 bit variable cannot cover one day */ +typedef int32_t seconds; +typedef int32_t microseconds; + +/** the time structure */ +typedef struct +{ + microseconds us; + seconds s; +} time_h; + + + +/**********************************************************/ + +/** init time module : schedule the event with the givent priority */ +void time_init(uint8_t priority); + +/**********************************************************/ + +/** get time in second since last init/reset */ +seconds time_get_s(void); + +/**********************************************************/ + +/** get time in microsecond since last init/reset */ +microseconds time_get_us(void); + +/**********************************************************/ + +/** get the complete time struct since last init/reset */ +time_h time_get_time(void); + +/**********************************************************/ + +/** reset time counter */ +void time_reset(void); + +/**********************************************************/ + +/** set time */ +void time_set(seconds s, microseconds us); + +/**********************************************************/ + +/** This is an equivalent of 'wait_ms(x)', but uses time value, so it + * is independant of CPU load. Warning, you should not use this + * function in a irq locked context, or in a scheduled function with + * higher priority than time module */ +void time_wait_ms(uint16_t ms); + +/**********************************************************/ + +/** get a microsecond timer that overflows naturally */ +microseconds time_get_us2(void); + +#endif diff --git a/modules/base/time/time.c b/modules/base/time/time.c index c44bcd4..af38b83 100644 --- a/modules/base/time/time.c +++ b/modules/base/time/time.c @@ -38,7 +38,7 @@ #include #include -#include +#include #include /**********************************************************/ diff --git a/modules/base/time/time.h b/modules/base/time/time.h deleted file mode 100644 index 4e1fcec..0000000 --- a/modules/base/time/time.h +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright Droids Corporation, Microb Technology, Eirbot (2005) - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Revision : $Id: time.h,v 1.3.4.2 2007-05-23 17:18:11 zer0 Exp $ - * - */ - -/* Droids-corp, Eirbot, Microb Technology 2005 - Zer0 - * Interface of the time module - */ - -/** - * This module can be used to get a human readable time. It uses the - * scheduler module. Its goal is not to be very precise, but just - * simple to use. provides two timers: one in s and us, and one in - * us which doesn't overflow on seconds (better to substract two - * times) - */ - -#ifndef _TIME_H_ -#define _TIME_H_ - -#include - -/* a 16 bit variable cannot cover one day */ -typedef int32_t seconds; -typedef int32_t microseconds; - - -/** the time structure */ -typedef struct -{ - microseconds us; - seconds s; -} time_h; - - - -/**********************************************************/ - -/** init time module : schedule the event with the givent priority */ -void time_init(uint8_t priority); - -/**********************************************************/ - -/** get time in second since last init/reset */ -seconds time_get_s(void); - -/**********************************************************/ - -/** get time in microsecond since last init/reset */ -microseconds time_get_us(void); - -/**********************************************************/ - -/** get the complete time struct since last init/reset */ -time_h time_get_time(void); - -/**********************************************************/ - -/** reset time counter */ -void time_reset(void); - -/**********************************************************/ - -/** set time */ -void time_set(seconds s, microseconds us); - -/**********************************************************/ - -/** This is an equivalent of 'wait_ms(x)', but uses time value, so it - * is independant of CPU load. Warning, you should not use this - * function in a irq locked context, or in a scheduled function with - * higher priority than time module */ -void time_wait_ms(uint16_t ms); - -/**********************************************************/ - -/** get a microsecond timer that overflows naturally */ -microseconds time_get_us2(void); - -#endif diff --git a/modules/devices/robot/obstacle_avoidance/obstacle_avoidance.c b/modules/devices/robot/obstacle_avoidance/obstacle_avoidance.c index ee8a655..5b19ed3 100755 --- a/modules/devices/robot/obstacle_avoidance/obstacle_avoidance.c +++ b/modules/devices/robot/obstacle_avoidance/obstacle_avoidance.c @@ -164,7 +164,7 @@ void oa_dump(void) printf_P(PSTR("poly #%d\r\n"), i); for (j=0; jl; j++) { pt = &poly->pts[j]; - printf_P(PSTR(" pt #%d (%"PRIi32",%"PRIi32")\r\n"), j, pt->x, pt->y); + printf_P(PSTR(" pt #%d (%2.2f,%2.2f)\r\n"), j, pt->x, pt->y); } } } diff --git a/projects/microb2010/tests/hostsim/.config b/projects/microb2010/tests/hostsim/.config index 47c7158..a66a449 100644 --- a/projects/microb2010/tests/hostsim/.config +++ b/projects/microb2010/tests/hostsim/.config @@ -1,5 +1,5 @@ # -# Automatically generated by make menuconfig: don't edit +# Automatically generated make config: don't edit # # @@ -74,6 +74,10 @@ CONFIG_FORMAT_IHEX=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 @@ -94,6 +98,10 @@ CONFIG_MODULE_TIME_CREATE_CONFIG=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 is not set @@ -178,6 +186,10 @@ CONFIG_MODULE_OBSTACLE_AVOIDANCE_CREATE_CONFIG=y # Control system modules # CONFIG_MODULE_CONTROL_SYSTEM_MANAGER=y + +# +# Filters +# CONFIG_MODULE_PID=y CONFIG_MODULE_PID_CREATE_CONFIG=y # CONFIG_MODULE_RAMP is not set @@ -188,12 +200,20 @@ CONFIG_MODULE_QUADRAMP=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 @@ -203,12 +223,20 @@ CONFIG_MODULE_QUADRAMP=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=y diff --git a/projects/microb2010/tests/hostsim/Makefile b/projects/microb2010/tests/hostsim/Makefile index ffc971e..187ae3a 100644 --- a/projects/microb2010/tests/hostsim/Makefile +++ b/projects/microb2010/tests/hostsim/Makefile @@ -18,6 +18,8 @@ SRC += actuator.c strat_avoid.c # care about how the name is spelled on its command-line. ASRC = +CFLAGS += -Wall -Werror + -include .aversive_conf include $(AVERSIVE_DIR)/mk/aversive_project.mk diff --git a/projects/microb2010/tests/hostsim/cmdline.c b/projects/microb2010/tests/hostsim/cmdline.c index 3038036..77392ec 100644 --- a/projects/microb2010/tests/hostsim/cmdline.c +++ b/projects/microb2010/tests/hostsim/cmdline.c @@ -31,7 +31,7 @@ #include #include #include -#include +#include #include #include diff --git a/projects/microb2010/tests/hostsim/commands_mainboard.c b/projects/microb2010/tests/hostsim/commands_mainboard.c index 0de0b2d..b07983b 100644 --- a/projects/microb2010/tests/hostsim/commands_mainboard.c +++ b/projects/microb2010/tests/hostsim/commands_mainboard.c @@ -30,7 +30,7 @@ #include #include -#include +#include #include #include diff --git a/projects/microb2010/tests/hostsim/commands_traj.c b/projects/microb2010/tests/hostsim/commands_traj.c index 55bfe5f..0e23798 100644 --- a/projects/microb2010/tests/hostsim/commands_traj.c +++ b/projects/microb2010/tests/hostsim/commands_traj.c @@ -29,7 +29,7 @@ #include #include -#include +#include #include #include diff --git a/projects/microb2010/tests/hostsim/cs.c b/projects/microb2010/tests/hostsim/cs.c index c5ecf27..c2d0ff4 100644 --- a/projects/microb2010/tests/hostsim/cs.c +++ b/projects/microb2010/tests/hostsim/cs.c @@ -29,7 +29,7 @@ #include #include -#include +#include #include #include diff --git a/projects/microb2010/tests/hostsim/main.c b/projects/microb2010/tests/hostsim/main.c index 85a31a2..31a7832 100644 --- a/projects/microb2010/tests/hostsim/main.c +++ b/projects/microb2010/tests/hostsim/main.c @@ -28,7 +28,6 @@ #include #include -#include #include #include @@ -42,11 +41,15 @@ #include #include +#include //#include #include #include "cs.h" #include "cmdline.h" +#include "robotsim.h" +#include "strat.h" +#include "strat_base.h" #include "main.h" struct genboard gen; @@ -74,13 +77,14 @@ int main(void) #ifdef CONFIG_MODULE_TIMER timer_init(); #endif - scheduler_init(); + printf("init\n"); -#ifdef HOST_VERSION + scheduler_init(); + hostsim_init(); robotsim_init(); -#endif + microb_cs_init(); time_init(TIME_PRIO); @@ -94,11 +98,6 @@ int main(void) cmdline_interact(); -/* trajectory_d_rel(&mainboard.traj, 1000); */ -/* time_wait_ms(2000); */ -/* trajectory_circle_rel(&mainboard.traj, 1500, 1000, */ -/* 250, 360, 0); */ -/* time_wait_ms(15000); */ return 0; } diff --git a/projects/microb2010/tests/hostsim/robotsim.c b/projects/microb2010/tests/hostsim/robotsim.c index 990104b..85224a3 100644 --- a/projects/microb2010/tests/hostsim/robotsim.c +++ b/projects/microb2010/tests/hostsim/robotsim.c @@ -22,8 +22,9 @@ #include #include #include -#include +#include #include +#include #include #include @@ -111,11 +112,13 @@ void robotsim_dump(void) char buf[BUFSIZ]; int len; - len =snprintf(buf, sizeof(buf), "%d %d %d\n", + len = snprintf(buf, sizeof(buf), "%d %d %d\n", position_get_x_s16(&mainboard.pos), position_get_y_s16(&mainboard.pos), position_get_a_deg_s16(&mainboard.pos)); + hostsim_lock(); write(fd, buf, len); + hostsim_unlock(); } void robotsim_pwm(void *arg, int32_t val) diff --git a/projects/microb2010/tests/hostsim/strat.c b/projects/microb2010/tests/hostsim/strat.c index ab6de1d..4cc17fb 100644 --- a/projects/microb2010/tests/hostsim/strat.c +++ b/projects/microb2010/tests/hostsim/strat.c @@ -31,7 +31,7 @@ #include #include -#include +#include #include #include diff --git a/projects/microb2010/tests/hostsim/strat_base.c b/projects/microb2010/tests/hostsim/strat_base.c index 0dd1a8b..19e9cf6 100644 --- a/projects/microb2010/tests/hostsim/strat_base.c +++ b/projects/microb2010/tests/hostsim/strat_base.c @@ -29,7 +29,7 @@ #include #include -#include +#include #include #include diff --git a/projects/microb2010/tests/hostsim/strat_utils.c b/projects/microb2010/tests/hostsim/strat_utils.c index 8fcecb2..57c5a0b 100644 --- a/projects/microb2010/tests/hostsim/strat_utils.c +++ b/projects/microb2010/tests/hostsim/strat_utils.c @@ -402,7 +402,7 @@ uint8_t wait_scan_done(uint16_t timeout) uint8_t opponent_is_behind(void) { int8_t opp_there; - int16_t opp_d, opp_a; + int16_t opp_d = 0, opp_a = 0; opp_there = get_opponent_da(&opp_d, &opp_a); if (opp_there && (opp_a < 215 && opp_a > 145) && opp_d < 600)