feat: Initialize backend with NestJS framework and serial communication

- Added package.json for backend dependencies and scripts.
- Implemented basic AppController and AppService with a simple "Hello World!" endpoint.
- Created SerialModule for handling serial communication with the Pico.
- Developed SerialController and SerialService to manage serial ports and commands.
- Implemented event streaming for serial data.
- Added Jest tests for AppController and e2e tests for the application.
- Set up TypeScript configuration for the backend.
- Created frontend SendBox component for sending commands to the backend.
- Established Pico firmware for controlling a stepper motor via serial commands.
- Documented project structure and usage in README files.
This commit is contained in:
2025-08-27 22:11:33 +02:00
parent bd41ffbe79
commit d607308b1d
32 changed files with 11622 additions and 165 deletions

5
pico/.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch

10
pico/.vscode/extensions.json vendored Normal file
View File

@@ -0,0 +1,10 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
]
}

View File

@@ -0,0 +1,35 @@
# Pico Serial Motor Control Integration
This project enables a Raspberry Pi Pico to be controlled via serial commands from a TypeScript backend. The Pico firmware parses commands received over serial and controls a stepper motor accordingly, while also sending log/status messages back.
## Serial Commands Supported
- `STEP <steps> <direction>`: Move the motor by the specified number of steps. Direction: 1 for forward, 0 for reverse.
- `SPEED <delay_us>`: Set the motor speed (delay in microseconds between steps).
- `LOG <message>`: Print a log message to serial.
## Firmware Files
- `src/main.cpp`: Main firmware logic, serial command parsing, motor control, logging.
- `src/ULN2003Stepper.h`, `src/Stepper.h`: Stepper motor driver classes.
## How to Extend
- Add new serial commands by updating the parser in `main.cpp`.
- Implement additional motor features or logging as needed.
- Ensure any new global variables are only defined in one source file, and declared as `extern` elsewhere.
## TypeScript Backend
- Use a library like `serialport` to communicate with the Pico.
- Send commands as plain text terminated by `\n`.
- Read and process log/status messages from serial.
## Example Serial Session
```
SPEED 3000
STEP 4096 1
LOG Motor moved
```
## Build & Upload
Use PlatformIO to build and upload the firmware to the Pico.
---
This README is intended for another developer or Copilot to implement further changes or features.

37
pico/include/README Normal file
View 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
pico/lib/README Normal file
View 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
pico/platformio.ini Normal file
View 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:pico]
platform = raspberrypi
board = pico
framework = arduino
; build_flags removed, no longer needed

15
pico/src/Stepper.h Normal file
View File

@@ -0,0 +1,15 @@
#pragma once
// ...existing code...
class Stepper {
protected:
int steps_per_rev;
public:
virtual void step(int steps, bool direction) = 0;
void step_rev(int revs, bool direction){
step(steps_per_rev*revs, direction);
}
int get_steps_per_rev(){
return steps_per_rev;
}
virtual ~Stepper() {}
};

39
pico/src/ULN2003Stepper.h Normal file
View File

@@ -0,0 +1,39 @@
#pragma once
#include <Arduino.h>
#include "Stepper.h"
#include <array>
class ULN2003Stepper : public Stepper {
private:
std::array<uint8_t, 4> pins;
static constexpr int steps_per_seq = 8;
const uint8_t sequence[8][4] = {
{1, 0, 0, 0},
{1, 1, 0, 0},
{0, 1, 0, 0},
{0, 1, 1, 0},
{0, 0, 1, 0},
{0, 0, 1, 1},
{0, 0, 0, 1},
{1, 0, 0, 1}
};
int step_delay_us = 5000;
public:
ULN2003Stepper(std::array<uint8_t, 4> pins, int rev_steps) : pins(pins) {
steps_per_rev = rev_steps;
for (auto pin : pins) {
pinMode(pin, OUTPUT);
}
}
void step(int steps, bool direction) override {
for (int i = 0; i < steps; ++i) {
int seq_idx = direction ? (i % steps_per_seq) : (steps_per_seq - 1 - (i % steps_per_seq));
for (int j = 0; j < 4; ++j) {
digitalWrite(pins[j], sequence[seq_idx][j]);
}
delayMicroseconds(step_delay_us);
}
}
void setSpeed(int delay_us) {
step_delay_us = delay_us;
}
};

53
pico/src/main.cpp Normal file
View File

@@ -0,0 +1,53 @@
#include <Arduino.h>
#include "Stepper.h"
#include "ULN2003Stepper.h"
ULN2003Stepper driver1({18, 19, 20, 21}, 4096);
int revSteps = 0;
void setup() {
Serial.begin(115200);
revSteps = driver1.get_steps_per_rev();
Serial.print("Steps: ");
Serial.println(revSteps);
}
void loop() {
static String input = "";
while (Serial.available()) {
char c = Serial.read();
if (c == '\n' || c == '\r') {
if (input.length() > 0) {
Serial.print("Received: ");
Serial.println(input);
// Parse command
if (input.startsWith("STEP ")) {
int idx1 = input.indexOf(' ');
int idx2 = input.indexOf(' ', idx1 + 1);
int steps = input.substring(idx1 + 1, idx2).toInt();
int dir = input.substring(idx2 + 1).toInt();
driver1.step(steps, dir != 0);
Serial.print("Motor moved ");
Serial.print(steps);
Serial.print(" steps in direction ");
Serial.println(dir);
} else if (input.startsWith("SPEED ")) {
int delay_us = input.substring(6).toInt();
driver1.setSpeed(delay_us);
Serial.print("Speed set to ");
Serial.println(delay_us);
} else if (input.startsWith("LOG ")) {
Serial.print("LOG: ");
Serial.println(input.substring(4));
} else {
Serial.println("Unknown command");
}
input = "";
}
} else {
input += c;
}
}
delay(10); // avoid busy loop
}

11
pico/test/README Normal file
View 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