From f6cb28cfba0a9c75ada9a01d65e50f2465404021 Mon Sep 17 00:00:00 2001 From: bskdany <54674556+bskdany@users.noreply.github.com> Date: Sun, 17 Nov 2024 15:02:48 -0500 Subject: [PATCH 1/5] Changed actuator state to be an atomic bool --- pad_server/src/actuator.c | 31 +++++++++++++++++++++++++++++-- pad_server/src/actuator.h | 13 +++++++------ pad_server/src/state.c | 36 ++++++++++-------------------------- pad_server/src/state.h | 2 +- 4 files changed, 47 insertions(+), 35 deletions(-) diff --git a/pad_server/src/actuator.c b/pad_server/src/actuator.c index d27646e..9e0bd96 100644 --- a/pad_server/src/actuator.c +++ b/pad_server/src/actuator.c @@ -40,14 +40,41 @@ void actuator_init(actuator_t *act, uint8_t id, actuate_f on, actuate_f off, voi * @param act The actuator to turn on. * @return 0 for success, an error code on failure. */ -int actuator_on(actuator_t *act) { return act->on(act); } +int actuator_on(actuator_t *act) { + int err = act->on(act); + if (err == -1) { + return err; + } + act->state = true; + return 0; +} /* * Turn the actuator off. * @param act The actuator to turn off. * @return 0 for an error code on failure. */ -int actuator_off(actuator_t *act) { return act->off(act); } +int actuator_off(actuator_t *act) { + int err = act->off(act); + if (err == -1) { + return err; + } + act->state = false; + return 0; +} + +/* + * Helper method to set the actuator status + * @param act The actuator + * @param new_state the new state for said actutator + */ +int actuator_set(actuator_t *act, bool new_state) { + if (new_state) { + return actuator_on(act); + } else { + return actuator_off(act); + } +} /* * Get the string name of the actuator. diff --git a/pad_server/src/actuator.h b/pad_server/src/actuator.h index 83d65b6..dc50767 100644 --- a/pad_server/src/actuator.h +++ b/pad_server/src/actuator.h @@ -1,6 +1,7 @@ #ifndef _ACTUATOR_H_ #define _ACTUATOR_H_ +#include #include #include @@ -34,17 +35,17 @@ typedef int (*actuate_f)(struct actuator *act); * Could be a valve, servo, etc. */ typedef struct actuator { - act_id_e id; /* The unique numeric ID of the actuator */ - bool state; /* The actuator state, true being on and false being off*/ - actuate_f on; /* Function to turn the actuator on. */ - actuate_f off; /* Function to turn the actuator off. */ - void *priv; /* Any private information needed by the actuator control functions */ + act_id_e id; /* The unique numeric ID of the actuator */ + atomic_bool state; /* The actuator state, true being on and false being off*/ + actuate_f on; /* Function to turn the actuator on. */ + actuate_f off; /* Function to turn the actuator off. */ + void *priv; /* Any private information needed by the actuator control functions */ } actuator_t; int actuator_on(actuator_t *act); int actuator_off(actuator_t *act); void actuator_init(actuator_t *act, uint8_t id, actuate_f on, actuate_f off, void *priv); -int actuator_set(actuator_t actuator, bool new_state); +int actuator_set(actuator_t *act, bool new_state); // const char *actuator_name(actuator_t *act); Conflicts with state.h, commenting for now. - Tony #endif // _ACTUATOR_H_ diff --git a/pad_server/src/state.c b/pad_server/src/state.c index 55343e2..1b406bc 100644 --- a/pad_server/src/state.c +++ b/pad_server/src/state.c @@ -54,31 +54,27 @@ int padstate_change_level(padstate_t *state, arm_lvl_e new_arm) { } /* - * Changes the state of the actuator in a thread safe way, no checks are done + * Changes the state of the actuator, no checks are done * @param id The actuator id * @param new_state The new actuator state * @return 0 for success, -1 for error */ int padstate_actuate(padstate_t *state, uint8_t id, bool new_state) { - int err; - err = pthread_rwlock_wrlock(&state->rw_lock); - if (err) return err; - state->actuators[id].state = new_state; - - return pthread_rwlock_unlock(&state->rw_lock); + return 0; } +/* + * Changes the state of the actuator, no checks are done + * @param id The actuator id + * @param new_state The new actuator state + * @return 0 for success, -1 for error + */ int padstate_get_actstate(padstate_t *state, uint8_t act_id, bool *act_state) { if (act_id >= NUM_ACTUATORS) return -1; - int err; - err = pthread_rwlock_rdlock(&state->rw_lock); - if (err) return err; - *act_state = state->actuators[act_id].state; - - return pthread_rwlock_unlock(&state->rw_lock); + return 0; } /* @@ -101,10 +97,7 @@ int pad_actuate(padstate_t *state, uint8_t id, uint8_t req_state) { return -1; } - fprintf(stderr, "arming level: %d\n", arm_lvl); - bool is_solenoid_valve = id >= ID_XV1 && id <= ID_XV12; - fprintf(stderr, "%ul\n", arm_lvl); switch (arm_lvl) { case ARMED_PAD: @@ -145,17 +138,8 @@ int pad_actuate(padstate_t *state, uint8_t id, uint8_t req_state) { return ACT_OK; } - err = padstate_actuate(state, id, new_state); - if (err) { - errno = err; - return -1; - } + err = actuator_set(&state->actuators[id], new_state); - if (new_state == true) { - err = actuator_on(&state->actuators[id]); - } else { - err = actuator_off(&state->actuators[id]); - } if (err) { return -1; } diff --git a/pad_server/src/state.h b/pad_server/src/state.h index ed0ec6c..d4311ac 100644 --- a/pad_server/src/state.h +++ b/pad_server/src/state.h @@ -21,7 +21,7 @@ typedef struct { void padstate_init(padstate_t *state); int padstate_get_level(padstate_t *state, arm_lvl_e *arm_val); int padstate_change_level(padstate_t *state, arm_lvl_e new_arm); -int padstate_actuate(padstate_t *state, uint8_t id, bool new_state); +int padstate_actuate(padstate_t *state, uint8_t act_id, bool new_state); int padstate_get_actstate(padstate_t *state, uint8_t act_id, bool *act_val); int pad_actuate(padstate_t *state, uint8_t id, uint8_t req_state); From c596042205ae8985c1a347c9a4254c5aaf9402e7 Mon Sep 17 00:00:00 2001 From: bskdany <54674556+bskdany@users.noreply.github.com> Date: Sun, 24 Nov 2024 22:30:41 -0500 Subject: [PATCH 2/5] Forgot how atomics work in C --- pad_server/src/actuator.c | 7 ++++--- pad_server/src/state.c | 6 ++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/pad_server/src/actuator.c b/pad_server/src/actuator.c index 9e0bd96..8332bd8 100644 --- a/pad_server/src/actuator.c +++ b/pad_server/src/actuator.c @@ -1,4 +1,5 @@ #include "state.h" +#include #include /* String names of the actuators. */ @@ -29,7 +30,7 @@ static const char *ACTUATOR_STR[] = { */ void actuator_init(actuator_t *act, uint8_t id, actuate_f on, actuate_f off, void *priv) { act->id = id; - act->state = false; + act->state = ATOMIC_VAR_INIT(false); act->on = on; act->off = off; act->priv = priv; @@ -45,7 +46,7 @@ int actuator_on(actuator_t *act) { if (err == -1) { return err; } - act->state = true; + atomic_store(&act->state, true); return 0; } @@ -59,7 +60,7 @@ int actuator_off(actuator_t *act) { if (err == -1) { return err; } - act->state = false; + atomic_store(&act->state, false); return 0; } diff --git a/pad_server/src/state.c b/pad_server/src/state.c index 1b406bc..7dac4ea 100644 --- a/pad_server/src/state.c +++ b/pad_server/src/state.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -60,7 +61,8 @@ int padstate_change_level(padstate_t *state, arm_lvl_e new_arm) { * @return 0 for success, -1 for error */ int padstate_actuate(padstate_t *state, uint8_t id, bool new_state) { - state->actuators[id].state = new_state; + if (id >= NUM_ACTUATORS) return -1; + atomic_store(&state->actuators[id].state, new_state); return 0; } @@ -73,7 +75,7 @@ int padstate_actuate(padstate_t *state, uint8_t id, bool new_state) { int padstate_get_actstate(padstate_t *state, uint8_t act_id, bool *act_state) { if (act_id >= NUM_ACTUATORS) return -1; - *act_state = state->actuators[act_id].state; + *act_state = atomic_load(&state->actuators[act_id].state); return 0; } From e9c9180702dd6cf2fe86d25e459e2b086d8022fb Mon Sep 17 00:00:00 2001 From: bskdany <54674556+bskdany@users.noreply.github.com> Date: Tue, 26 Nov 2024 10:23:13 -0500 Subject: [PATCH 3/5] Removed padstate_actuate in favor of pad_actuate --- pad_server/src/state.c | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/pad_server/src/state.c b/pad_server/src/state.c index 7dac4ea..d553352 100644 --- a/pad_server/src/state.c +++ b/pad_server/src/state.c @@ -54,18 +54,6 @@ int padstate_change_level(padstate_t *state, arm_lvl_e new_arm) { return pthread_rwlock_unlock(&state->rw_lock); } -/* - * Changes the state of the actuator, no checks are done - * @param id The actuator id - * @param new_state The new actuator state - * @return 0 for success, -1 for error - */ -int padstate_actuate(padstate_t *state, uint8_t id, bool new_state) { - if (id >= NUM_ACTUATORS) return -1; - atomic_store(&state->actuators[id].state, new_state); - return 0; -} - /* * Changes the state of the actuator, no checks are done * @param id The actuator id From 3c0c9bdaffd94d3a31b18aaffc8d5396f7b52f20 Mon Sep 17 00:00:00 2001 From: Elias Hawa <58529029+EliasJRH@users.noreply.github.com> Date: Tue, 26 Nov 2024 10:24:40 -0500 Subject: [PATCH 4/5] Remove padstate_actuate from header --- pad_server/src/state.h | 1 - 1 file changed, 1 deletion(-) diff --git a/pad_server/src/state.h b/pad_server/src/state.h index d4311ac..48a7dcd 100644 --- a/pad_server/src/state.h +++ b/pad_server/src/state.h @@ -21,7 +21,6 @@ typedef struct { void padstate_init(padstate_t *state); int padstate_get_level(padstate_t *state, arm_lvl_e *arm_val); int padstate_change_level(padstate_t *state, arm_lvl_e new_arm); -int padstate_actuate(padstate_t *state, uint8_t act_id, bool new_state); int padstate_get_actstate(padstate_t *state, uint8_t act_id, bool *act_val); int pad_actuate(padstate_t *state, uint8_t id, uint8_t req_state); From d8be033a0ded3f5b098ccac57c9a5647876e0073 Mon Sep 17 00:00:00 2001 From: Elias Hawa <58529029+EliasJRH@users.noreply.github.com> Date: Tue, 26 Nov 2024 10:28:08 -0500 Subject: [PATCH 5/5] Update doc for get_actstate --- pad_server/src/state.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pad_server/src/state.c b/pad_server/src/state.c index d553352..0c760c6 100644 --- a/pad_server/src/state.c +++ b/pad_server/src/state.c @@ -55,9 +55,9 @@ int padstate_change_level(padstate_t *state, arm_lvl_e new_arm) { } /* - * Changes the state of the actuator, no checks are done + * Obtains state of actuator given id using * @param id The actuator id - * @param new_state The new actuator state + * @param act_state Variable to store actuator state * @return 0 for success, -1 for error */ int padstate_get_actstate(padstate_t *state, uint8_t act_id, bool *act_state) {