Skip to content

Commit

Permalink
Gyro sensor polling via timers
Browse files Browse the repository at this point in the history
implement sensor temperature and motion polling via RTOS tasks,
publish data to event loop
  • Loading branch information
vortigont committed Mar 26, 2024
1 parent 9181882 commit fa0608b
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 24 deletions.
6 changes: 5 additions & 1 deletion SolderingPen_ESP32S2/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//
#include "main.h"
#include "evtloop.h"
#include "ts.h"
#include "heater.hpp"
#include "sensors.hpp"
Expand Down Expand Up @@ -157,6 +156,11 @@ void setup() {
Serial.begin(115200);
Serial.setTxTimeoutMs(0);

#if PTS200_DEBUG_LEVEL > 3
// let ACM device intitialize
delay(3000);
#endif

// Start event loop task
evt::start();
// event bus sniffer
Expand Down
72 changes: 55 additions & 17 deletions SolderingPen_ESP32S2/sensors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,50 @@
#include "log.h"

#define LIS
#define MOTION_FACTOR 25000
#define MOTION_POLL_PERIOD 50 // ms
#define ACCEL_MOTION_FACTOR 25000
#define ACCEL_MOTION_POLL_PERIOD 50 // ms
#define ACCEL_TEMPERATURE_POLL_PERIOD 1000 // ms


GyroSensor::~GyroSensor(){
ts.deleteTask(_tPoller);
xTimerDelete( _tmr_temp, portMAX_DELAY );
xTimerDelete( _tmr_accel, portMAX_DELAY );
_tmr_temp = nullptr;
_tmr_accel = nullptr;
};

void GyroSensor::begin(){
if (!accel.begin()) {
LOGE(T_GYRO, println, "Accelerometer not detected.");
}

_tPoller.set(MOTION_POLL_PERIOD, TASK_FOREVER, [this](){ _poll(); });
ts.addTask(_tPoller);
_tPoller.enable();
// _tPoller.set(ACCEL_MOTION_POLL_PERIOD, TASK_FOREVER, [this](){ _poll(); });
// ts.addTask(_tPoller);
// _tPoller.enable();

// start temperature polling
if (!_tmr_temp){
_tmr_temp = xTimerCreate("gyroT",
pdMS_TO_TICKS(ACCEL_TEMPERATURE_POLL_PERIOD),
pdTRUE,
static_cast<void*>(this),
[](TimerHandle_t h) { static_cast<GyroSensor*>(pvTimerGetTimerID(h))->_temperature_poll(); }
);
if (_tmr_temp)
xTimerStart( _tmr_temp, pdMS_TO_TICKS(10) );
}

// start accl polling
if (!_tmr_accel){
_tmr_accel = xTimerCreate("gyroA",
pdMS_TO_TICKS(ACCEL_MOTION_POLL_PERIOD),
pdTRUE,
static_cast<void*>(this),
[](TimerHandle_t h) { static_cast<GyroSensor*>(pvTimerGetTimerID(h))->_accel_poll(); }
);
// keep it disabled on begin
//xTimerStart( _tmr_accel, pdMS_TO_TICKS(10) );
}

// lis2dh12_block_data_update_set(&(accel.dev_ctx), PROPERTY_DISABLE);
// accel.setScale(LIS2DH12_2g);
Expand All @@ -32,7 +61,7 @@ bool GyroSensor::motionDetect(){
return out;
}

void GyroSensor::_poll(){
void GyroSensor::_accel_poll(){
/*#if defined(MPU)
mpu6050.update();
if (abs(mpu6050.getGyroX() - gx) > WAKEUP_THRESHOLD ||
Expand Down Expand Up @@ -76,7 +105,7 @@ void GyroSensor::_poll(){
// Serial.print(" Z: ");
// Serial.println(accels[accelIndex][2]);

if (accelIndex != ACCEL_SAMPLES)
if (accelIndex != GYRO_ACCEL_SAMPLES)
return;

accelIndex = 0;
Expand All @@ -88,9 +117,9 @@ void GyroSensor::_poll(){
avg[2] += i.z;
}

avg[0] /= ACCEL_SAMPLES;
avg[1] /= ACCEL_SAMPLES;
avg[2] /= ACCEL_SAMPLES;
avg[0] /= GYRO_ACCEL_SAMPLES;
avg[1] /= GYRO_ACCEL_SAMPLES;
avg[2] /= GYRO_ACCEL_SAMPLES;

uint32_t var[3] = {0, 0, 0};
for (auto &i : samples){
Expand All @@ -99,9 +128,9 @@ void GyroSensor::_poll(){
var[2] += (i.z - avg[2]) * (i.z - avg[2]);
}

var[0] /= ACCEL_SAMPLES;
var[1] /= ACCEL_SAMPLES;
var[2] /= ACCEL_SAMPLES;
var[0] /= GYRO_ACCEL_SAMPLES;
var[1] /= GYRO_ACCEL_SAMPLES;
var[2] /= GYRO_ACCEL_SAMPLES;

// debug output
// Serial.print("variance: ");
Expand All @@ -111,13 +140,15 @@ void GyroSensor::_poll(){
// Serial.print(" ");
// Serial.println(var[2]);

int varThreshold = _motionThreshold * MOTION_FACTOR;
int varThreshold = _motionThreshold * ACCEL_MOTION_FACTOR;

if (var[0] > varThreshold || var[1] > varThreshold || var[2] > varThreshold) {
_motion = true;
_clear();
LOGD(T_GYRO, println, "motion detected!");
LOGD(T_GYRO, printf, "Th:%d, x:%u, y:%u, z:%u\n", varThreshold, var[0], var[1], var[2]);
// post event with motion detect
EVT_POST(SENSOR_DATA, e2int(evt::iron_t::motion));
}
}

Expand All @@ -133,11 +164,13 @@ float GyroSensor::getAccellTemp() {

void GyroSensor::enable(){
_clear();
_tPoller.enable();
if (_tmr_accel)
xTimerStart( _tmr_accel, pdMS_TO_TICKS(10) );
}

void GyroSensor::disable(){
_tPoller.disable();
if (_tmr_accel)
xTimerStop( _tmr_accel, pdMS_TO_TICKS(10) );
}

void GyroSensor::_clear(){
Expand All @@ -148,3 +181,8 @@ void GyroSensor::_clear(){

samples.fill(axis);
}

void GyroSensor::_temperature_poll(){
float t = getAccellTemp();
EVT_POST_DATA(SENSOR_DATA, e2int(evt::iron_t::acceltemp), &t, sizeof(t));
}
28 changes: 23 additions & 5 deletions SolderingPen_ESP32S2/sensors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
// https://github.com/sparkfun/SparkFun_LIS2DH12_Arduino_Library
#include "SparkFun_LIS2DH12.h"
#include "ts.h"
#include "evtloop.hpp"
#include "freertos/timers.h"

#define ACCEL_SAMPLES 32
#define GYRO_ACCEL_SAMPLES 32

class GyroSensor {
// G sensor axis metrics
Expand All @@ -15,22 +17,37 @@ class GyroSensor {

bool _motion{false};
int _motionThreshold{WAKEUP_THRESHOLD};
std::array<Gaxis, ACCEL_SAMPLES> samples;
std::array<Gaxis, GYRO_ACCEL_SAMPLES> samples;
size_t accelIndex{0};
//uint16_t accels[32][3];

SPARKFUN_LIS2DH12 accel;
Task _tPoller;

void _poll();
// temperature polling timer
TimerHandle_t _tmr_temp = nullptr;
// accell sensor polling timer
TimerHandle_t _tmr_accel = nullptr;

/**
* @brief clear sampling array
*
*/
void _clear();

/**
* @brief poll temperature sensor inside accell chip and publish to event message bus
*
*/
void _temperature_poll();

/**
* @brief poll gyro sensor and detect motion
*
*/
void _accel_poll();


public:
// d-tor
~GyroSensor();

/**
Expand Down Expand Up @@ -77,3 +94,4 @@ class GyroSensor {
*/
float getAccellTemp();
};

11 changes: 10 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,19 @@ lib_deps =
mike-matera/FastPID @ ~1.3
sparkfun/SparkFun LIS2DH12 Arduino Library @ ^1.0.3
arkhipenko/TaskScheduler @ ~3.7
build_src_flags =
-std=gnu++17
build_unflags =
-std=gnu++11
;build_flags =
; -std=gnu++17


[env:pts200debug]
extends = env:pts200
build_flags =
build_src_flags =
${env:pts200.build_src_flags}
-Wextra
;DEBUG_LEVEL severity level: 1 = error, 2 = warning, 3 = info, 4 = debug, 5 = verbose
-DPTS200_DEBUG_LEVEL=4
;-D CORE_DEBUG_LEVEL=3

0 comments on commit fa0608b

Please sign in to comment.