return motor speed from poll function (dirty af)

This commit is contained in:
Joeri Exelmans 2025-06-04 17:38:09 +02:00
parent 10d3de62da
commit e8f246cfa5
3 changed files with 30 additions and 17 deletions

View file

@ -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));
}