Update to high speed encoder stream
This commit is contained in:
@@ -12,3 +12,4 @@
|
||||
platform = atmelavr
|
||||
board = megaatmega2560
|
||||
framework = arduino
|
||||
monitor_speed = 921600
|
||||
|
||||
@@ -1,54 +1,228 @@
|
||||
// Copyright (C) 2026 Pierre Barbier <pierrebarbier741@gmail.com>
|
||||
// Copyright (C) 2026 Association Exergie <association.exergie@gmail.com>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#define PIN_REG 2
|
||||
#define PIN_SYNC 3
|
||||
#define REG_SIZE 256
|
||||
#define SYNC_SIZE 256
|
||||
#ifndef ARDUINO_ISR_ATTR
|
||||
#define ARDUINO_ISR_ATTR
|
||||
#endif
|
||||
|
||||
uint32_t REG_BUFF[REG_SIZE];
|
||||
uint32_t SYNC_BUFF[SYNC_SIZE];
|
||||
constexpr uint8_t TURN_PIN = 27;
|
||||
constexpr uint8_t PULSE_PIN = 26;
|
||||
constexpr uint8_t GP_PINS[] = {32, 33};
|
||||
|
||||
volatile uint8_t REG_IR = 0;
|
||||
volatile uint8_t REG_IW = 0;
|
||||
volatile uint8_t SYNC_IR = 0;
|
||||
volatile uint8_t SYNC_IW = 0;
|
||||
constexpr uint32_t BAUD_RATE = 921600;
|
||||
constexpr size_t EVENT_BUFFER_SIZE = 256;
|
||||
constexpr size_t GP_INPUT_COUNT = sizeof(GP_PINS) / sizeof(GP_PINS[0]);
|
||||
constexpr size_t PACKET_PULSE_COUNT = 16;
|
||||
constexpr size_t INPUT_PULSES_PER_TURN = 1024;
|
||||
constexpr size_t PULSE_DECIMATION = 4;
|
||||
constexpr size_t OUTPUT_PULSES_PER_TURN = INPUT_PULSES_PER_TURN / PULSE_DECIMATION;
|
||||
constexpr uint32_t UINT16_DELTA_MAX = UINT16_MAX;
|
||||
|
||||
void append_buff_reg();
|
||||
void append_buff_sync();
|
||||
static_assert(INPUT_PULSES_PER_TURN % PULSE_DECIMATION == 0);
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
constexpr uint8_t TURN_MAGIC[] = {0xE7, 0x54, 0xC3, 0xA1};
|
||||
constexpr uint8_t PULSE16_MAGIC[] = {0xE7, 0x50, 0xC3, 0xA1};
|
||||
constexpr uint8_t PULSE32_MAGIC[] = {0xE7, 0x70, 0xC3, 0xA1};
|
||||
constexpr uint8_t GP_MAGIC[] = {0xE7, 0x47, 0xC3, 0xA1};
|
||||
|
||||
attachInterrupt(digitalPinToInterrupt(PIN_REG), append_buff_reg, FALLING);
|
||||
attachInterrupt(digitalPinToInterrupt(PIN_SYNC), append_buff_sync, FALLING);
|
||||
volatile uint32_t PULSE_REG[EVENT_BUFFER_SIZE];
|
||||
volatile uint8_t PULSE_W_HEAD = 0;
|
||||
uint8_t PULSE_R_HEAD = 0;
|
||||
|
||||
volatile uint32_t TURN_REG[EVENT_BUFFER_SIZE];
|
||||
volatile uint8_t TURN_W_HEAD = 0;
|
||||
uint8_t TURN_R_HEAD = 0;
|
||||
|
||||
volatile uint32_t GP_REG[GP_INPUT_COUNT][EVENT_BUFFER_SIZE];
|
||||
volatile uint8_t GP_W_HEAD[GP_INPUT_COUNT] = {};
|
||||
uint8_t GP_R_HEAD[GP_INPUT_COUNT] = {};
|
||||
|
||||
uint32_t packet_deltas[PACKET_PULSE_COUNT];
|
||||
size_t packet_count = 0;
|
||||
size_t raw_turn_pulse_count = 0;
|
||||
size_t output_turn_pulse_count = 0;
|
||||
uint32_t previous_timestamp = 0;
|
||||
bool have_active_turn = false;
|
||||
|
||||
void ARDUINO_ISR_ATTR handle_pulse_isr() {
|
||||
PULSE_REG[PULSE_W_HEAD] = micros();
|
||||
PULSE_W_HEAD = PULSE_W_HEAD + 1;
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (Serial.availableForWrite() >= 5) {
|
||||
if (REG_IW != REG_IR) {
|
||||
Serial.write('R');
|
||||
Serial.write((uint8_t *)®_BUFF[REG_IR], sizeof(uint32_t));
|
||||
REG_IR++;
|
||||
void ARDUINO_ISR_ATTR handle_turn_isr() {
|
||||
TURN_REG[TURN_W_HEAD] = micros();
|
||||
TURN_W_HEAD = TURN_W_HEAD + 1;
|
||||
}
|
||||
|
||||
void ARDUINO_ISR_ATTR handle_gp_falling(size_t input) {
|
||||
GP_REG[input][GP_W_HEAD[input]] = micros();
|
||||
GP_W_HEAD[input] = GP_W_HEAD[input] + 1;
|
||||
}
|
||||
|
||||
void ARDUINO_ISR_ATTR handle_gp0_isr() {
|
||||
handle_gp_falling(0);
|
||||
}
|
||||
|
||||
void ARDUINO_ISR_ATTR handle_gp1_isr() {
|
||||
handle_gp_falling(1);
|
||||
}
|
||||
|
||||
bool timestamp_is_before(uint32_t lhs, uint32_t rhs) {
|
||||
return static_cast<int32_t>(lhs - rhs) < 0;
|
||||
}
|
||||
|
||||
void write_magic(const uint8_t magic[4]) {
|
||||
Serial.write(magic, 4);
|
||||
}
|
||||
|
||||
void write_u16(uint16_t value) {
|
||||
Serial.write(reinterpret_cast<const uint8_t *>(&value), sizeof(value));
|
||||
}
|
||||
|
||||
void write_u32(uint32_t value) {
|
||||
Serial.write(reinterpret_cast<const uint8_t *>(&value), sizeof(value));
|
||||
}
|
||||
|
||||
void send_packet(size_t used_count) {
|
||||
bool needs_u32 = false;
|
||||
|
||||
for (size_t index = 0; index < PACKET_PULSE_COUNT; index++) {
|
||||
if (index >= used_count) {
|
||||
packet_deltas[index] = 0;
|
||||
}
|
||||
|
||||
if (SYNC_IW != SYNC_IR) {
|
||||
Serial.write('S');
|
||||
Serial.write((uint8_t *)&SYNC_BUFF[SYNC_IR], sizeof(uint32_t));
|
||||
SYNC_IR++;
|
||||
if (packet_deltas[index] > UINT16_DELTA_MAX) {
|
||||
needs_u32 = true;
|
||||
}
|
||||
}
|
||||
|
||||
write_magic(needs_u32 ? PULSE32_MAGIC : PULSE16_MAGIC);
|
||||
|
||||
if (needs_u32) {
|
||||
for (size_t index = 0; index < PACKET_PULSE_COUNT; index++) {
|
||||
write_u32(packet_deltas[index]);
|
||||
}
|
||||
} else {
|
||||
for (size_t index = 0; index < PACKET_PULSE_COUNT; index++) {
|
||||
write_u16(static_cast<uint16_t>(packet_deltas[index]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void append_buff_reg() {
|
||||
REG_BUFF[REG_IW] = micros();
|
||||
REG_IW++;
|
||||
void flush_partial_packet() {
|
||||
if (packet_count == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
send_packet(packet_count);
|
||||
packet_count = 0;
|
||||
}
|
||||
|
||||
void append_buff_sync() {
|
||||
SYNC_BUFF[SYNC_IW] = micros();
|
||||
SYNC_IW++;
|
||||
void start_turn(uint32_t timestamp) {
|
||||
if (have_active_turn) {
|
||||
flush_partial_packet();
|
||||
}
|
||||
|
||||
write_magic(TURN_MAGIC);
|
||||
write_u32(timestamp);
|
||||
|
||||
have_active_turn = true;
|
||||
previous_timestamp = timestamp;
|
||||
packet_count = 0;
|
||||
raw_turn_pulse_count = 0;
|
||||
output_turn_pulse_count = 0;
|
||||
}
|
||||
|
||||
void handle_pulse(uint32_t timestamp) {
|
||||
if (!have_active_turn || raw_turn_pulse_count >= INPUT_PULSES_PER_TURN) {
|
||||
return;
|
||||
}
|
||||
|
||||
const bool should_emit = raw_turn_pulse_count % PULSE_DECIMATION == 0;
|
||||
raw_turn_pulse_count++;
|
||||
|
||||
if (!should_emit || output_turn_pulse_count >= OUTPUT_PULSES_PER_TURN) {
|
||||
return;
|
||||
}
|
||||
|
||||
packet_deltas[packet_count] = timestamp - previous_timestamp;
|
||||
previous_timestamp = timestamp;
|
||||
packet_count++;
|
||||
output_turn_pulse_count++;
|
||||
|
||||
if (packet_count == PACKET_PULSE_COUNT) {
|
||||
send_packet(PACKET_PULSE_COUNT);
|
||||
packet_count = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void handle_gp(size_t channel, uint32_t timestamp) {
|
||||
if (!have_active_turn) {
|
||||
return;
|
||||
}
|
||||
|
||||
write_magic(GP_MAGIC);
|
||||
Serial.write(static_cast<uint8_t>(channel));
|
||||
write_u32(timestamp);
|
||||
}
|
||||
|
||||
void process_next_event() {
|
||||
enum EventType { NONE, TURN, PULSE, GP };
|
||||
|
||||
EventType next_type = NONE;
|
||||
uint32_t next_timestamp = 0;
|
||||
size_t next_gp_channel = 0;
|
||||
|
||||
if (TURN_R_HEAD != TURN_W_HEAD) {
|
||||
next_type = TURN;
|
||||
next_timestamp = TURN_REG[TURN_R_HEAD];
|
||||
}
|
||||
|
||||
if (PULSE_R_HEAD != PULSE_W_HEAD &&
|
||||
(next_type == NONE ||
|
||||
timestamp_is_before(PULSE_REG[PULSE_R_HEAD], next_timestamp))) {
|
||||
next_type = PULSE;
|
||||
next_timestamp = PULSE_REG[PULSE_R_HEAD];
|
||||
}
|
||||
|
||||
for (size_t channel = 0; channel < GP_INPUT_COUNT; channel++) {
|
||||
if (GP_R_HEAD[channel] == GP_W_HEAD[channel]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const uint32_t timestamp = GP_REG[channel][GP_R_HEAD[channel]];
|
||||
if (next_type == NONE || timestamp_is_before(timestamp, next_timestamp)) {
|
||||
next_type = GP;
|
||||
next_timestamp = timestamp;
|
||||
next_gp_channel = channel;
|
||||
}
|
||||
}
|
||||
|
||||
switch (next_type) {
|
||||
case TURN:
|
||||
start_turn(next_timestamp);
|
||||
TURN_R_HEAD++;
|
||||
break;
|
||||
case PULSE:
|
||||
handle_pulse(next_timestamp);
|
||||
PULSE_R_HEAD++;
|
||||
break;
|
||||
case GP:
|
||||
handle_gp(next_gp_channel, next_timestamp);
|
||||
GP_R_HEAD[next_gp_channel]++;
|
||||
break;
|
||||
case NONE:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(BAUD_RATE);
|
||||
attachInterrupt(digitalPinToInterrupt(TURN_PIN), handle_turn_isr, RISING);
|
||||
attachInterrupt(digitalPinToInterrupt(PULSE_PIN), handle_pulse_isr, FALLING);
|
||||
attachInterrupt(digitalPinToInterrupt(GP_PINS[0]), handle_gp0_isr, FALLING);
|
||||
attachInterrupt(digitalPinToInterrupt(GP_PINS[1]), handle_gp1_isr, FALLING);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
process_next_event();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user