mirror of
https://github.com/Vale54321/schafkop-neu.git
synced 2025-12-16 11:49:34 +01:00
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:
15
pico/src/Stepper.h
Normal file
15
pico/src/Stepper.h
Normal 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
39
pico/src/ULN2003Stepper.h
Normal 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
53
pico/src/main.cpp
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user