Update to high speed encoder stream
This commit is contained in:
5
ESP32/.gitignore
vendored
Normal file
5
ESP32/.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
.pio
|
||||
.vscode/.browse.c_cpp.db*
|
||||
.vscode/c_cpp_properties.json
|
||||
.vscode/launch.json
|
||||
.vscode/ipch
|
||||
37
ESP32/include/README
Normal file
37
ESP32/include/README
Normal file
@@ -0,0 +1,37 @@
|
||||
|
||||
This directory is intended for project header files.
|
||||
|
||||
A header file is a file containing C declarations and macro definitions
|
||||
to be shared between several project source files. You request the use of a
|
||||
header file in your project source file (C, C++, etc) located in `src` folder
|
||||
by including it, with the C preprocessing directive `#include'.
|
||||
|
||||
```src/main.c
|
||||
|
||||
#include "header.h"
|
||||
|
||||
int main (void)
|
||||
{
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
Including a header file produces the same results as copying the header file
|
||||
into each source file that needs it. Such copying would be time-consuming
|
||||
and error-prone. With a header file, the related declarations appear
|
||||
in only one place. If they need to be changed, they can be changed in one
|
||||
place, and programs that include the header file will automatically use the
|
||||
new version when next recompiled. The header file eliminates the labor of
|
||||
finding and changing all the copies as well as the risk that a failure to
|
||||
find one copy will result in inconsistencies within a program.
|
||||
|
||||
In C, the convention is to give header files names that end with `.h'.
|
||||
|
||||
Read more about using header files in official GCC documentation:
|
||||
|
||||
* Include Syntax
|
||||
* Include Operation
|
||||
* Once-Only Headers
|
||||
* Computed Includes
|
||||
|
||||
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
|
||||
46
ESP32/lib/README
Normal file
46
ESP32/lib/README
Normal file
@@ -0,0 +1,46 @@
|
||||
|
||||
This directory is intended for project specific (private) libraries.
|
||||
PlatformIO will compile them to static libraries and link into the executable file.
|
||||
|
||||
The source code of each library should be placed in a separate directory
|
||||
("lib/your_library_name/[Code]").
|
||||
|
||||
For example, see the structure of the following example libraries `Foo` and `Bar`:
|
||||
|
||||
|--lib
|
||||
| |
|
||||
| |--Bar
|
||||
| | |--docs
|
||||
| | |--examples
|
||||
| | |--src
|
||||
| | |- Bar.c
|
||||
| | |- Bar.h
|
||||
| | |- library.json (optional. for custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
|
||||
| |
|
||||
| |--Foo
|
||||
| | |- Foo.c
|
||||
| | |- Foo.h
|
||||
| |
|
||||
| |- README --> THIS FILE
|
||||
|
|
||||
|- platformio.ini
|
||||
|--src
|
||||
|- main.c
|
||||
|
||||
Example contents of `src/main.c` using Foo and Bar:
|
||||
```
|
||||
#include <Foo.h>
|
||||
#include <Bar.h>
|
||||
|
||||
int main (void)
|
||||
{
|
||||
...
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
The PlatformIO Library Dependency Finder will find automatically dependent
|
||||
libraries by scanning project source files.
|
||||
|
||||
More information about PlatformIO Library Dependency Finder
|
||||
- https://docs.platformio.org/page/librarymanager/ldf.html
|
||||
15
ESP32/platformio.ini
Normal file
15
ESP32/platformio.ini
Normal file
@@ -0,0 +1,15 @@
|
||||
; PlatformIO Project Configuration File
|
||||
;
|
||||
; Build options: build flags, source filter
|
||||
; Upload options: custom upload port, speed and extra flags
|
||||
; Library options: dependencies, extra library storages
|
||||
; Advanced options: extra scripting
|
||||
;
|
||||
; Please visit documentation for the other options and examples
|
||||
; https://docs.platformio.org/page/projectconf.html
|
||||
|
||||
[env:esp32dev]
|
||||
platform = espressif32
|
||||
board = esp32dev
|
||||
framework = arduino
|
||||
monitor_speed = 921600
|
||||
224
ESP32/src/main.cpp
Normal file
224
ESP32/src/main.cpp
Normal file
@@ -0,0 +1,224 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
constexpr uint8_t TURN_PIN = 27;
|
||||
constexpr uint8_t PULSE_PIN = 26;
|
||||
constexpr uint8_t GP_PINS[] = {32, 33};
|
||||
|
||||
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;
|
||||
|
||||
static_assert(INPUT_PULSES_PER_TURN % PULSE_DECIMATION == 0);
|
||||
|
||||
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};
|
||||
|
||||
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 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 (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 flush_partial_packet() {
|
||||
if (packet_count == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
send_packet(packet_count);
|
||||
packet_count = 0;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
11
ESP32/test/README
Normal file
11
ESP32/test/README
Normal file
@@ -0,0 +1,11 @@
|
||||
|
||||
This directory is intended for PlatformIO Test Runner and project tests.
|
||||
|
||||
Unit Testing is a software testing method by which individual units of
|
||||
source code, sets of one or more MCU program modules together with associated
|
||||
control data, usage procedures, and operating procedures, are tested to
|
||||
determine whether they are fit for use. Unit testing finds problems early
|
||||
in the development cycle.
|
||||
|
||||
More information about PlatformIO Unit Testing:
|
||||
- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html
|
||||
Reference in New Issue
Block a user