diff --git a/robot_cdt/Makefile b/robot_cdt/Makefile index 75db5ef..f20b2ad 100644 --- a/robot_cdt/Makefile +++ b/robot_cdt/Makefile @@ -18,8 +18,8 @@ build/%.o: %.c create_build_dirs $(TARGET): $(OBJS) $(CC) $(CFLAGS) -o build/$@ $^ -# all: $(TARGET) -all: $(OBJS) +all: $(TARGET) +# all: $(OBJS) create_build_dirs: mkdir -p build/src diff --git a/robot_cdt/Statechart.ysc b/robot_cdt/Statechart.ysc index 4a0bc63..550bc0a 100644 --- a/robot_cdt/Statechart.ysc +++ b/robot_cdt/Statechart.ysc @@ -1,6 +1,6 @@ - + diff --git a/robot_cdt/src/controller.c b/robot_cdt/src/controller.c index 356081f..7ecea62 100644 --- a/robot_cdt/src/controller.c +++ b/robot_cdt/src/controller.c @@ -14,20 +14,25 @@ static sc_timer_t timers[MAX_TIMERS]; static sc_timer_service_t timer_service; +// Observers are stateless (they just link to callbacks) so it's safe to use global variables here +static sc_single_subscription_observer_sc_integer observer_motorL; +static sc_single_subscription_observer_sc_integer observer_motorR; + +static sc_integer motor_left_speed; +static sc_integer motor_right_speed; + // event handler callback functions -void on_motorL(Statechart* sc, sc_real value) { +void on_motorL(Statechart* sc, sc_integer value) { fprintf(stderr, "got event motorL: %f\n", value); + motor_left_speed = value; } -void on_motorR(Statechart* sc, sc_real value) { +void on_motorR(Statechart* sc, sc_integer value) { fprintf(stderr, "got event motorR: %f\n", value); + motor_right_speed = value; } // called once, on startup -void controller_init( - Statechart* sc, - sc_single_subscription_observer_sc_real* observer_motorL, - sc_single_subscription_observer_sc_real* observer_motorR -) { +void controller_init(Statechart* sc) { sc_timer_service_init(&timer_service, timers, MAX_TIMERS, (sc_raise_time_event_fp) &statechart_raise_time_event); @@ -37,24 +42,27 @@ void controller_init( // subscribe to output events: // motorL observer - sc_single_subscription_observer_sc_real_init(observer_motorL, sc, (sc_observer_next_sc_real_fp) on_motorL); - sc_single_subscription_observer_sc_real_subscribe(observer_motorL, statechart_get_setMotorL(sc)); + sc_single_subscription_observer_sc_integer_init(&observer_motorL, sc, (sc_observer_next_sc_integer_fp) on_motorL); + sc_single_subscription_observer_sc_integer_subscribe(&observer_motorL, statechart_get_setMotorL(sc)); // motorR observer - sc_single_subscription_observer_sc_real_init(observer_motorR, sc, (sc_observer_next_sc_real_fp) on_motorR); - sc_single_subscription_observer_sc_real_subscribe(observer_motorR, statechart_get_setMotorR(sc)); + sc_single_subscription_observer_sc_integer_init(&observer_motorR, sc, (sc_observer_next_sc_integer_fp) on_motorR); + sc_single_subscription_observer_sc_integer_subscribe(&observer_motorR, statechart_get_setMotorR(sc)); } // to be called every 10 ms -void controller_poll() { +void controller_poll(sc_integer* _motor_left_speed, sc_integer* _motor_right_speed) { sc_time elapsed = 10; // ms sc_timer_service_proceed(&timer_service, elapsed); fprintf(stderr, "made 10ms step\n"); + + (*_motor_left_speed) = motor_left_speed; + (*_motor_right_speed) = motor_right_speed; } // finally, we need to implemented these functions (defined in "Statechart_required.h") -sc_real statechart_pid(Statechart* handle, const sc_real sensor, pid_vars_t pid_vars) { - sc_real error = TARGET_DIST - sensor; // is this correct? +sc_integer statechart_pid(Statechart* handle, const sc_integer sensor, pid_vars_t pid_vars) { + sc_integer error = TARGET_DIST - sensor; // is this correct? return pid(pid_vars, error); } // here you see the reason we need global variables for the timer service: @@ -65,3 +73,8 @@ void statechart_set_timer(Statechart *sc, const sc_eventid evid, const sc_intege void statechart_unset_timer(Statechart *sc, const sc_eventid evid) { sc_timer_unset(&timer_service, evid); } + + +void main() { + fprintf(stderr, "%d", sizeof(Statechart)); +}