feat: add semaphore

This commit is contained in:
Wlad Meixner 2022-09-30 11:20:45 +02:00
parent 3cd1060252
commit fc23b17ccd
2 changed files with 38 additions and 1 deletions

View File

@ -1,6 +1,6 @@
import os
Import("env")
os.system("python32 .pio/libdeps/esp32dev/Nanopb/generator/nanopb_generator.py proto/message.proto --strip-path")
os.system("python3 .pio/libdeps/esp32dev/Nanopb/generator/nanopb_generator.py proto/message.proto --strip-path")
os.system("mv ./proto/message.pb.c ./src/message.pb.c")
os.system("mv ./proto/message.pb.h ./include/message.pb.h")

View File

@ -18,6 +18,7 @@
#define UP_BTN_PIN 27
#define DOWN_BTN_PIN 13
#define M_ENABLE_PIN 5
// define event bits for movement of individual motors
#define DIR_REVERSE_BIT (1UL << 0UL)
@ -31,6 +32,9 @@
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
int runningMotors = 0;
static SemaphoreHandle_t bin_sem;
MotorControl *motors[] = {
new MotorControl(0, 22, 23),
new MotorControl(1, 18, 19),
@ -89,6 +93,13 @@ void motorTask(void *pvParameter) {
// - button pressed
// - no spin command was already started
if (direction != 0) {
xSemaphoreTake(bin_sem, portMAX_DELAY);
if (runningMotors == 0) {
digitalWrite(M_ENABLE_PIN, LOW);
}
runningMotors++;
xSemaphoreGive(bin_sem);
m->stepper->startRotate(direction * rotationAngle);
while (direction != 0) {
@ -105,6 +116,13 @@ void motorTask(void *pvParameter) {
}
}
m->stepper->stop();
xSemaphoreTake(bin_sem, portMAX_DELAY);
runningMotors--;
if (runningMotors == 0) {
digitalWrite(M_ENABLE_PIN, HIGH);
}
xSemaphoreGive(bin_sem);
}
// target based motor handling
@ -115,6 +133,13 @@ void motorTask(void *pvParameter) {
// NOTE: maybe adjust speed here
m->stepper->startMove(diff);
xSemaphoreTake(bin_sem, portMAX_DELAY);
if (runningMotors == 0) {
digitalWrite(M_ENABLE_PIN, LOW);
}
runningMotors++;
xSemaphoreGive(bin_sem);
while (true) {
// motor control loop - send pulse and return how long to wait until next pulse
motorWaitTimeMicros = m->stepper->nextAction();
@ -130,6 +155,12 @@ void motorTask(void *pvParameter) {
}
}
m->updateCurrentPosition(ongoingStepTarget);
xSemaphoreTake(bin_sem, portMAX_DELAY);
runningMotors--;
if (runningMotors == 0) {
digitalWrite(M_ENABLE_PIN, HIGH);
}
xSemaphoreGive(bin_sem);
}
vTaskDelay(noActionIdleTime);
@ -188,6 +219,12 @@ extern "C" void app_main() {
// initialize arduino library before we start the tasks
initArduino();
// Create mutexes and semaphores before starting tasks
bin_sem = xSemaphoreCreateBinary();
pinMode(M_ENABLE_PIN, OUTPUT);
digitalWrite(M_ENABLE_PIN, HIGH);
BLEDevice::init("GrowMe-beta-1");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);