added as5600 sensor readout

This commit is contained in:
2023-01-01 18:16:14 +01:00
parent 0828ced6d1
commit 01be1b6a93
3 changed files with 39 additions and 134 deletions

View File

@@ -15,6 +15,7 @@ framework = arduino
lib_deps =
bblanchon/ArduinoJson@^6.18.5
fastled/FastLED@^3.4.0
https://github.com/RobTillaart/AS5600.git
monitor_speed = 115200
lib_ldf_mode = deep+
extra_scripts = ./littlefsbuilder.py

View File

@@ -1,5 +1,7 @@
#include "sensor.h"
AS5600 as5600; // use default Wire
ADS1115 ADS(0x48);
Battery battery(VBATTMIN, VBATTMAX, MEAS_ADC, &getvbatt);
@@ -69,12 +71,24 @@ void initBattery(void)
battery.begin(VBATTREF, (R12 + R13) / R13, &sigmoidal); // R1 = 220K, R2 = 100K, factor = (R1+R2)/R2
}
void initAS5600(void)
{
as5600.begin(); // set direction pin.
as5600.setDirection(AS5600_CLOCK_WISE); // default, just be explicit.
int b = as5600.isConnected();
Serial.print("Connect: ");
Serial.println(b);
}
void initSensor(void)
{
log_i("sensor init ADS1x15:");
bool result = ADS.begin(I2C_SDA, I2C_SCL);
ADS.setMode(0);
initBattery();
initAS5600();
if (!result)
{
log_e("sensor init: FAIL");
@@ -84,6 +98,8 @@ void initSensor(void)
log_i("sensor init: OK");
hallinitOK = true;
}
}
bool CheckBattery(void)
@@ -150,144 +166,27 @@ void handleBatterySensor(void)
HALLSENSORSTATES getprogressstate(uint16_t sample)
{
if (int(sample - last_hall_sample) > HALLTHRESHOLD)
{
return hall_increasing;
}
else if (int(sample - last_hall_sample) < HALLTHRESHOLD)
{
return hall_decreasing;
}
else
{
return hall_idle;
}
return hall_idle;
}
void handleHallSensor(void)
{
uint32_t timeNow = millis();
if (timeNow - last_hall_read > HALLINTERVAL)
readsensor();
int angle = as5600.getCumulativePosition();
log_i("sensor angle %d", angle);
if( angle > HALLTHRESHOLD)
{
// get sample
uint16_t hall_sample = ADS.readADC(HALL_INPUT);
switch (hall_sensor_state)
{
case hall_idle:
{
hall_is_Idle = true;
if (int(hall_sample - last_hall_sample) > HALLIDLETHRESHOLD)
{
hall_decrease_count = 0;
if (hall_increase_count++ > HALLIDLETHRESHOLD)
hall_sensor_state = hall_increasing;
}
else if (int(hall_sample - last_hall_sample) < HALLTHRESHOLD)
{
hall_increase_count = 0;
if (hall_decrease_count++ > HALLIDLETHRESHOLD)
hall_sensor_state = hall_decreasing;
}
}
break;
case hall_decreasing:
{
hall_is_Idle = false;
if (int(hall_sample - last_hall_sample) > HALLIDLETHRESHOLD)
{
hall_decrease_count = 0;
log_i("goto hall_increasing");
if (hall_increase_count++ > HALLIDLETHRESHOLD)
hall_sensor_state = hall_increasing;
}
else if (int(hall_sample - last_hall_sample) < HALLTHRESHOLD)
{
hall_increase_count = 0;
if(hall_decrease_count < HALLIDLETHRESHOLD) hall_decrease_count++;
}
else if (hall_decrease_count == 0)
{
hall_sensor_state = hall_tipover;
log_i("go to tipover");
}
else
{
hall_decrease_count--;
}
}
break;
case hall_tipover:
{
// check if samples increase
// check if samples decrease
// check if samples are steady
}
break;
case hall_increasing:
{
}
break;
default:
{
log_d("state: default");
}
break;
}
log_d("state: %s (sample: %d), countup(%d), countdown(%d)",
getHallSensorStateStr(hall_sensor_state).c_str(),
hall_sample,
hall_increase_count,
hall_decrease_count);
// bool skipfirstSample = false;
// if (!last_hall_Delta)
// {
// skipfirstSample = true;
// }
// uint16_t hall_delta = (last_hall_sample > hall_sample) ? (last_hall_sample - hall_sample) : (hall_sample - last_hall_sample);
// hall_delta = (hall_delta + last_hall_Delta) / 2;
// last_hall_Delta = hall_delta;
// if (skipfirstSample)
// {
// log_v("First sample skipped");
// if (hall_idle_count)
// {
// hall_idle_count--;
// }
// return;
// }
// if (hall_delta > HALLIDLETHRESHOLD)
// {
// if (hall_idle_count > HALLIDLESAMPLES)
// {
// hall_is_Idle = false;
// hall_idle_count = HALLPLAYSAMPLES;
// log_i("Game: playing, delta = %d", hall_delta);
// }
// else
// {
// hall_idle_count++;
// }
// }
// else
// {
// if (hall_idle_count == 0)
// {
// hall_is_Idle = true;
// }
// else
// {
// hall_idle_count--;
// }
// }
log_v("HallSensor: val=%d, delta=%d, count=%d, idle=%s\n",
hall_sample,
hall_delta,
hall_idle_count,
(hall_is_Idle ? "yes" : "no"));
last_hall_sample = hall_sample;
last_hall_read = timeNow;
hall_is_Idle = false;
}
else
{
hall_is_Idle = true;
}
as5600.resetPosition();
}
void readsensor(void)
{
}

View File

@@ -5,6 +5,8 @@
#include "Battery.h"
#include "board.h"
#include "led.h"
#include "AS5600.h"
#include "Wire.h"
#define ADSINTERVAL 100
#define VBATTINTERVALL 15000
@@ -16,6 +18,7 @@
#define HALLIDLETHRESHOLD 20
#define HALLIDLESAMPLES 15
#define HALLPLAYSAMPLES 24
#define HALLTHRESHOLD 5
typedef enum
{
@@ -38,5 +41,7 @@ bool getSensorInitStatus(void);
bool CheckBattery(void);
bool getLowBatt(void);
void readsensor(void);