Skip to content

Commit

Permalink
Replace task with mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
eamars committed Apr 29, 2024
1 parent c9431f0 commit 63c5510
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 47 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"common.h": "c",
"clocks.h": "c",
"pwm.h": "c",
"servo_gate.h": "c"
"servo_gate.h": "c",
"semphr.h": "c"
}
}
5 changes: 5 additions & 0 deletions src/html/web_portal.html
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,11 @@
<input type="number" class="input input-bordered" name="c4" step="0.0001">
</div>

<div class="grid grid-cols-1 gap-1">
<span class="label-text">Servo Gate Dwell Time (ms)</span>
<input type="number" class="input input-bordered" name="c5" step="1">
</div>

<button class="btn btn-neutral settings-apply-btn">Apply</button>
</form>
</section>
Expand Down
74 changes: 33 additions & 41 deletions src/servo_gate.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include <stdbool.h>
#include <math.h>
#include "servo_gate.h"
#include "hardware/pwm.h"
#include "hardware/clocks.h"
#include "configuration.h"
#include "eeprom.h"
#include "common.h"
#include "servo_gate.h"

// Attributes
servo_gate_t servo_gate;
Expand All @@ -22,14 +22,15 @@ const eeprom_servo_gate_config_t default_eeprom_servo_gate_config = {
.shutter0_open_duty_cycle = 0.05,
.shutter1_close_duty_cycle = 0.05,
.shutter1_open_duty_cycle = 0.09,
.servo_gate_dwell_time_ms = 200ul,
};


void _servo_gate_set_state(gate_state_t state) {
void _servo_gate_set_current_state() {
uint32_t shutter0_duty_cycle;
uint32_t shutter1_duty_cycle;

switch (state)
switch (servo_gate.gate_state)
{
case GATE_CLOSE:
shutter0_duty_cycle = _pwm_full_scale_level * servo_gate.eeprom_servo_gate_config.shutter0_close_duty_cycle;
Expand All @@ -55,22 +56,21 @@ void _servo_gate_set_state(gate_state_t state) {
}


void servo_gate_set_state(gate_state_t state) {
if (servo_gate.servo_gate_control_queue) {
xQueueSend(servo_gate.servo_gate_control_queue, &state, portMAX_DELAY);
}
}
void servo_gate_set_state(gate_state_t state, bool block_for_dwell) {
if (servo_gate.servo_gate_control_mux) {
xSemaphoreTake(servo_gate.servo_gate_control_mux, portMAX_DELAY);

servo_gate.gate_state = state;

void servo_gate_control_task(void * p) {
while (true) {
gate_state_t new_state;
xQueueReceive(servo_gate.servo_gate_control_queue, &new_state, portMAX_DELAY);
// Apply the state
_servo_gate_set_current_state();

servo_gate.gate_state = new_state;
if (block_for_dwell) {
BaseType_t scheduler_state = xTaskGetSchedulerState();
delay_ms(servo_gate.eeprom_servo_gate_config.servo_gate_dwell_time_ms, scheduler_state);
}

// Apply the new state
_servo_gate_set_state(new_state);
xSemaphoreGive(servo_gate.servo_gate_control_mux);
}
}

Expand Down Expand Up @@ -145,18 +145,9 @@ bool servo_gate_init() {
pwm_init(pwm_gpio_to_slice_num(SERVO1_PWM_PIN), &cfg, true);

// Start the RTOS task and queue
servo_gate.servo_gate_control_queue = xQueueCreate(1, sizeof(gate_state_t));

xTaskCreate(
servo_gate_control_task,
"servo_gate_controller",
configMINIMAL_STACK_SIZE,
NULL,
8,
&servo_gate.servo_gate_control_task_handler
);
servo_gate.servo_gate_control_mux = xSemaphoreCreateMutex();

servo_gate_set_state(servo_gate.gate_state);
servo_gate_set_state(servo_gate.gate_state, false);

return true;
}
Expand All @@ -172,7 +163,7 @@ bool http_rest_servo_gate_state(struct fs_file *file, int num_params, char *para
for (int idx = 0; idx < num_params; idx += 1) {
if (strcmp(params[idx], "g0") == 0) {
gate_state_t state = (gate_state_t) atoi(values[idx]);
servo_gate_set_state(state);
servo_gate_set_state(state, false);
}
}

Expand All @@ -196,10 +187,11 @@ bool http_rest_servo_gate_state(struct fs_file *file, int num_params, char *para
bool http_rest_servo_gate_config(struct fs_file *file, int num_params, char *params[], char *values[]) {
// Mappings
// c0 (bool): servo_gate_enable
// c1 (int): shutter0_close_duty_cycle
// c2 (int): shutter0_open_duty_cycle
// c3 (int): shutter1_close_duty_cycle
// c4 (int): shutter1_open_duty_cycle
// c1 (float): shutter0_close_duty_cycle
// c2 (float): shutter0_open_duty_cycle
// c3 (float): shutter1_close_duty_cycle
// c4 (float): shutter1_open_duty_cycle
// c5 (int | uint32_t): servo_gate_dwell_time_ms
// ee (bool): save_to_eeprom

static char servo_gate_json_buffer[256];
Expand All @@ -213,20 +205,19 @@ bool http_rest_servo_gate_config(struct fs_file *file, int num_params, char *par
servo_gate.eeprom_servo_gate_config.servo_gate_enable = enable;
}
else if (strcmp(params[idx], "c1") == 0) {
float duty_cycle = strtof(values[idx], NULL);
servo_gate.eeprom_servo_gate_config.shutter0_close_duty_cycle = duty_cycle;
servo_gate.eeprom_servo_gate_config.shutter0_close_duty_cycle = strtof(values[idx], NULL);;
}
else if (strcmp(params[idx], "c2") == 0) {
float duty_cycle = strtof(values[idx], NULL);
servo_gate.eeprom_servo_gate_config.shutter0_open_duty_cycle = duty_cycle;
servo_gate.eeprom_servo_gate_config.shutter0_open_duty_cycle = strtof(values[idx], NULL);;
}
else if (strcmp(params[idx], "c3") == 0) {
float duty_cycle = strtof(values[idx], NULL);
servo_gate.eeprom_servo_gate_config.shutter1_close_duty_cycle = duty_cycle;
servo_gate.eeprom_servo_gate_config.shutter1_close_duty_cycle = strtof(values[idx], NULL);;
}
else if (strcmp(params[idx], "c4") == 0) {
float duty_cycle = strtof(values[idx], NULL);
servo_gate.eeprom_servo_gate_config.shutter1_open_duty_cycle = duty_cycle;
servo_gate.eeprom_servo_gate_config.shutter1_open_duty_cycle = strtof(values[idx], NULL);;
}
else if (strcmp(params[idx], "c5") == 0) {
servo_gate.eeprom_servo_gate_config.servo_gate_dwell_time_ms = strtol(values[idx], NULL, 10);;
}
else if (strcmp(params[idx], "ee") == 0) {
save_to_eeprom = string_to_boolean(values[idx]);
Expand All @@ -242,13 +233,14 @@ bool http_rest_servo_gate_config(struct fs_file *file, int num_params, char *par
snprintf(servo_gate_json_buffer,
sizeof(servo_gate_json_buffer),
"%s"
"{\"c0\":%s,\"c1\":%0.3f,\"c2\":%0.3f,\"c3\":%0.3f,\"c4\":%0.3f}",
"{\"c0\":%s,\"c1\":%0.3f,\"c2\":%0.3f,\"c3\":%0.3f,\"c4\":%0.3f,\"c5\":%ld}",
http_json_header,
boolean_to_string(servo_gate.eeprom_servo_gate_config.servo_gate_enable),
servo_gate.eeprom_servo_gate_config.shutter0_close_duty_cycle,
servo_gate.eeprom_servo_gate_config.shutter0_open_duty_cycle,
servo_gate.eeprom_servo_gate_config.shutter1_close_duty_cycle,
servo_gate.eeprom_servo_gate_config.shutter1_open_duty_cycle);
servo_gate.eeprom_servo_gate_config.shutter1_open_duty_cycle,
servo_gate.eeprom_servo_gate_config.servo_gate_dwell_time_ms);

size_t data_length = strlen(servo_gate_json_buffer);
file->data = servo_gate_json_buffer;
Expand Down
12 changes: 7 additions & 5 deletions src/servo_gate.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

#include <stdint.h>
#include <FreeRTOS.h>
#include <queue.h>
#include <semphr.h>
#include "http_rest.h"

#define EEPROM_SERVO_GATE_CONFIG_REV 2 // 16 byte
#define EEPROM_SERVO_GATE_CONFIG_REV 1 // 16 byte

typedef enum {
GATE_DISABLED = 0,
Expand All @@ -22,6 +22,9 @@ typedef struct {
float shutter0_open_duty_cycle;
float shutter1_close_duty_cycle;
float shutter1_open_duty_cycle;

// Time taken to open or close the servo gate
uint32_t servo_gate_dwell_time_ms;
} eeprom_servo_gate_config_t;


Expand All @@ -30,8 +33,7 @@ typedef struct {
gate_state_t gate_state;

// RTOS control
TaskHandle_t servo_gate_control_task_handler;
QueueHandle_t servo_gate_control_queue;
xSemaphoreHandle servo_gate_control_mux;
} servo_gate_t;


Expand All @@ -44,7 +46,7 @@ bool servo_gate_config_save(void);
bool http_rest_servo_gate_state(struct fs_file *file, int num_params, char *params[], char *values[]);
bool http_rest_servo_gate_config(struct fs_file *file, int num_params, char *params[], char *values[]);

void servo_gate_set_state(gate_state_t state);
void servo_gate_set_state(gate_state_t, bool);

#ifdef __cplusplus
}
Expand Down

0 comments on commit 63c5510

Please sign in to comment.