Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed actuator state to be an atomic bool #42

Merged
merged 5 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions pad_server/src/actuator.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "state.h"
#include <stdatomic.h>
#include <stdint.h>

/* String names of the actuators. */
Expand Down Expand Up @@ -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;
Expand All @@ -40,14 +41,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;
}
atomic_store(&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;
}
atomic_store(&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.
Expand Down
13 changes: 7 additions & 6 deletions pad_server/src/actuator.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef _ACTUATOR_H_
#define _ACTUATOR_H_

#include <stdatomic.h>
#include <stdbool.h>
#include <stdint.h>

Expand Down Expand Up @@ -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_
38 changes: 6 additions & 32 deletions pad_server/src/state.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <errno.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
Expand Down Expand Up @@ -54,31 +55,16 @@ 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
* 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_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);
}

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);
*act_state = atomic_load(&state->actuators[act_id].state);
return 0;
}

/*
Expand All @@ -101,10 +87,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:
Expand Down Expand Up @@ -145,17 +128,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;
}
Expand Down
1 change: 0 additions & 1 deletion pad_server/src/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 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);

Expand Down