Skip to content

Commit

Permalink
add taskPriority and pin to core
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulaScharf committed Dec 4, 2024
1 parent 48d8c3c commit 300840f
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#include "AccelerationSensor.h"
#include "edge-impulse-sdk/classifier/ei_run_classifier.h"

AccelerationSensor::AccelerationSensor() : BaseSensor("accelerationSensorTask", 8192, 0) {}
AccelerationSensor::AccelerationSensor() : BaseSensor("accelerationSensorTask",
4096, // taskStackSize,
30, // taskDelay,
17, // taskPriority,
1 // core
) {}

String surfaceClassificationUUID = "b944af10-f495-4560-968f-2f0d18cab521";
// String accUUID = "B944AF10F4954560968F2F0D18CAB522";
Expand Down Expand Up @@ -43,8 +48,7 @@ float probSett = 0.0;
float probStanding = 0.0;
float anomaly = 0.0;

float prevAccTime = millis();

unsigned long startAccTime = millis();
bool AccelerationSensor::readSensorData()
{
bool classified = false;
Expand All @@ -59,8 +63,9 @@ bool AccelerationSensor::readSensorData()
buffer[ix++] = 1.0;
buffer[ix++] = 1.0;

// Serial.println(millis() - prevAccTime);
prevAccTime = millis();
unsigned long endAccTime = millis();
Serial.printf("acceleration: %lu ms\n", endAccTime - startAccTime);
startAccTime = millis();

// one second inverval
if (EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE <= ix)
Expand Down Expand Up @@ -103,6 +108,8 @@ bool AccelerationSensor::readSensorData()
ix = 0;

buffer[EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE] = {};

startAccTime = millis();
}

if (measurementCallback)
Expand Down
15 changes: 11 additions & 4 deletions sensebox-bike-atrai-v2-esp32s3/src/sensors/BaseSensor.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "BaseSensor.h"

BaseSensor::BaseSensor(const char *taskName, uint32_t taskStackSize, uint32_t taskDelay)
: activeSubscription(false), sendBLE(false), taskStackSize(taskStackSize), taskDelay(taskDelay) {}
BaseSensor::BaseSensor(const char *taskName, uint32_t taskStackSize, uint32_t taskDelay, uint32_t taskPriority, uint16_t core)
: activeSubscription(false), sendBLE(false), taskStackSize(taskStackSize), taskDelay(taskDelay), taskPriority(taskPriority), core(core) {}

static SemaphoreHandle_t i2c_mutex;

Expand All @@ -23,7 +23,7 @@ void BaseSensor::startSubscription()
if (this->taskHandle == NULL)
{
// https://docs.espressif.com/projects/esp-idf/en/v5.2.3/esp32s3/api-guides/performance/speed.html#choosing-task-priorities-of-the-application
xTaskCreate(sensorTask, taskName, taskStackSize, this, 17, &this->taskHandle);
xTaskCreatePinnedToCore(sensorTask, taskName, taskStackSize, this, taskPriority, &this->taskHandle, core);
// very important tasks could run on priority 20 on core 0
}
activeSubscription = true;
Expand All @@ -48,11 +48,13 @@ void BaseSensor::stopBLE()
sendBLE = false;
}


void BaseSensor::sensorTask(void *pvParameters)
{
BaseSensor *sensor = static_cast<BaseSensor *>(pvParameters);
while (true)
{
unsigned long prevTime = millis();
if (sensor->activeSubscription)
{
if (xSemaphoreTake(i2c_mutex, portMAX_DELAY) == pdTRUE)
Expand All @@ -61,6 +63,11 @@ void BaseSensor::sensorTask(void *pvParameters)
xSemaphoreGive(i2c_mutex);
}
}
vTaskDelay(pdMS_TO_TICKS(sensor->taskDelay));

if ((millis() - prevTime) < sensor->taskDelay)
{
vTaskDelay(pdMS_TO_TICKS(sensor->taskDelay - (millis() - prevTime)));
}
// vTaskDelay(pdMS_TO_TICKS(sensor->taskDelay));
}
}
4 changes: 3 additions & 1 deletion sensebox-bike-atrai-v2-esp32s3/src/sensors/BaseSensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class BaseSensor
{
public:
BaseSensor(const char *taskName, uint32_t taskStackSize = 8192, uint32_t taskDelay = 1000);
BaseSensor(const char *taskName, uint32_t taskStackSize = 8192, uint32_t taskDelay = 1000, uint32_t taskPriority = 1, uint16_t core = 1);

void begin();
void subscribe(std::function<void(std::vector<float>)> callback);
Expand All @@ -31,6 +31,8 @@ class BaseSensor
uint32_t taskStackSize;
uint32_t taskDelay;
TaskHandle_t taskHandle;
uint32_t taskPriority;
uint16_t core;
};

#endif // BASESENSOR_H
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#include "BatterySensor.h"

BatterySensor::BatterySensor() : BaseSensor("batterySensorTask", 8192, 1000) {}
BatterySensor::BatterySensor() : BaseSensor("batterySensorTask",
4096, // taskStackSize,
1000, // taskDelay,
1, // taskPriority,
1 // core
) {}

String batteryUUID = "5b262dea-4565-4ea0-912f-1e453bda0ca7";
int batteryCharacteristic = 0;
Expand All @@ -21,6 +26,7 @@ void BatterySensor::initSensor()
// add more if needed
}

unsigned long startBatTime = millis();
bool BatterySensor::readSensorData()
{
// read sensor data
Expand All @@ -31,6 +37,9 @@ bool BatterySensor::readSensorData()
measurementCallback({batteryCharge});
}

unsigned long endBatTime = millis();
Serial.printf("battery: %lu ms\n", endBatTime - startBatTime);
startBatTime = millis();
if (sendBLE)
{
notifyBLE(batteryCharge);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@
#include "edge-impulse-sdk/tensorflow/lite/schema/schema_generated.h"
#include <edge-impulse-sdk/tensorflow/lite/micro/micro_error_reporter.h>

DistanceSensor::DistanceSensor() : BaseSensor("distanceTask", 8192, 0) {}
DistanceSensor::DistanceSensor() : BaseSensor("distanceTask",
8192, // taskStackSize,
0, // taskDelay,
20, // taskPriority,
0 // core
) {}

// String distanceUUID = "B3491B60C0F34306A30D49C91F37A62B";
String distanceUUID = "b3491b60-c0f3-4306-a30d-49c91f37a62b";
Expand Down Expand Up @@ -110,6 +115,7 @@ void DistanceSensor::initSensor()
Wire.setClock(100000); // Sensor has max I2C freq of 1MHz
}

unsigned long startDisTime = millis();
bool DistanceSensor::readSensorData()
{
Wire.setClock(1000000); // Sensor has max I2C freq of 1MHz
Expand All @@ -123,7 +129,7 @@ bool DistanceSensor::readSensorData()
if ((!status) && (NewDataReady != 0))
{
// Serial.println("data ready");
Serial.println(millis()-prevDistanceTime);
// Serial.println(millis()-prevDistanceTime);
prevDistanceTime = millis();
sensor_vl53l8cx_top.get_ranging_data(&Results);
float overtakingPredictionPercentage = -1.0;
Expand Down Expand Up @@ -188,8 +194,8 @@ bool DistanceSensor::readSensorData()
{
const float *prediction_scores = interpreter->output(0)->data.f;
overtakingPredictionPercentage = prediction_scores[0];
Serial.print(overtakingPredictionPercentage);
Serial.print(" ");
// Serial.print(overtakingPredictionPercentage);
// Serial.print(" ");
}
}

Expand All @@ -202,15 +208,19 @@ bool DistanceSensor::readSensorData()
measurementCallback({distance, overtakingPredictionPercentage});
}

unsigned long endDisTime = millis();
Serial.printf("distance: %lu ms\n", endDisTime - startDisTime);
startDisTime = millis();

if (sendBLE)
{
notifyBLE(distance, overtakingPredictionPercentage);
}
}
if ((millis() - prevDistanceTime) < 65)
{
vTaskDelay(pdMS_TO_TICKS(65 - (millis() - prevDistanceTime)));
}
// if ((millis() - prevDistanceTime) < 65)
// {
// vTaskDelay(pdMS_TO_TICKS(65 - (millis() - prevDistanceTime)));
// }
// Serial.print("distance: ");
// Serial.println(millis() - prevDistanceTime);
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#include "DustSensor.h"

DustSensor::DustSensor() : BaseSensor("DustSensorTask", 8192, 1000) {}
DustSensor::DustSensor() : BaseSensor("DustSensorTask",
4096, // taskStackSize,
1000, // taskDelay,
1, // taskPriority,
1 // core
) {}

String dustUUID = "7e14e070-84ea-489f-b45a-e1317364b979";
int dustCharacteristic = 0;
Expand Down Expand Up @@ -41,6 +46,7 @@ void DustSensor::initSensor()
dustCharacteristic = BLEModule::createCharacteristic(dustUUID.c_str());
}

unsigned long startDusTime = millis();
bool DustSensor::readSensorData()
{
// Wire.setClock(100000); // Sensor has max I2C freq of 1MHz
Expand Down Expand Up @@ -77,6 +83,10 @@ bool DustSensor::readSensorData()
measurementCallback({pm1, pm2_5, pm4, pm10});
}

unsigned long endDusTime = millis();
Serial.printf("dust: %lu ms\n", endDusTime - startDusTime);
startDusTime = millis();

if (sendBLE)
{
notifyBLE(pm1, pm2_5, pm4, pm10);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#include "SampleSensor.h"

SampleSensor::SampleSensor() : BaseSensor("sampleSensorTask", 2048, 1000) {}
SampleSensor::SampleSensor() : BaseSensor("sampleSensorTask",
2048, // taskStackSize
1000, // taskDelay
1, // taskPriority
1 // core
) {}

String sampleUUID = "00000000000000000";
int sampleCharacteristic = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#include "TempHumiditySensor.h"

TempHumiditySensor::TempHumiditySensor() : BaseSensor("temperatureHumidityTask", 8192, 0) {}
TempHumiditySensor::TempHumiditySensor() : BaseSensor("temperatureHumidityTask",
4096, // taskStackSize
1000, // taskDelay
1, // taskPriority
1 // core
) {}

String tempUUID = "2cdf2174-35be-fdc4-4Ca2-6fd173f8b3a8";
String humUUID = "772df7ec-8cdc-4ea9-86af-410abe0ba257";
Expand All @@ -22,7 +27,7 @@ void TempHumiditySensor::initSensor()
temperatureCharacteristic = BLEModule::createCharacteristic(tempUUID.c_str());
humidityCharacteristic = BLEModule::createCharacteristic(humUUID.c_str());
}

unsigned long startTemTime = millis();
bool TempHumiditySensor::readSensorData()
{
float temperature = hdc.readTemperature();
Expand All @@ -39,6 +44,9 @@ bool TempHumiditySensor::readSensorData()
measurementCallback({temperature, humidity});
}

unsigned long endTemTime = millis();
Serial.printf("temperature/humidity: %lu ms\n", endTemTime - startTemTime);
startTemTime = millis();
if (sendBLE)
{
notifyBLE(temperature, humidity);
Expand Down

0 comments on commit 300840f

Please sign in to comment.