adding backend script + test program for microcontroller
This commit is contained in:
10
arduino_code/Test/.vscode/extensions.json
vendored
Normal file
10
arduino_code/Test/.vscode/extensions.json
vendored
Normal 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"
|
||||
]
|
||||
}
|
||||
37
arduino_code/Test/include/README
Normal file
37
arduino_code/Test/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
arduino_code/Test/lib/README
Normal file
46
arduino_code/Test/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
|
||||
19
arduino_code/Test/platformio.ini
Normal file
19
arduino_code/Test/platformio.ini
Normal file
@@ -0,0 +1,19 @@
|
||||
; 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:upesy_wroom]
|
||||
platform = espressif32
|
||||
board = upesy_wroom
|
||||
framework = arduino
|
||||
monitor_speed = 115200
|
||||
upload_speed = 921600
|
||||
upload_port = /dev/ttyUSB0
|
||||
lib_deps =
|
||||
knolleary/PubSubClient@^2.8
|
||||
129
arduino_code/Test/src/main.cpp
Normal file
129
arduino_code/Test/src/main.cpp
Normal file
@@ -0,0 +1,129 @@
|
||||
// arduino_code/Test/src/main.cpp
|
||||
#include <Arduino.h>
|
||||
#include <WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
|
||||
// WiFi credentials
|
||||
const char* ssid = "Home";
|
||||
const char* password = "88888888qwE";
|
||||
|
||||
// MQTT Server details
|
||||
const char* mqtt_server = "192.168.31.225";
|
||||
const char* mqtt_user = "test";
|
||||
const char* mqtt_password = "1234";
|
||||
|
||||
WiFiClient espClient;
|
||||
PubSubClient client(espClient);
|
||||
|
||||
// Пины для управления TMC2209
|
||||
const int DIR_PIN = 18; // Направление
|
||||
const int STEP_PIN = 19; // Шаг
|
||||
const int EN_PIN = 21; // Включение (опционально)
|
||||
|
||||
// Переменные для управления
|
||||
bool motor_running = false;
|
||||
int step_delay = 1000; // Задержка между шагами (в микросекундах)
|
||||
int steps_per_second = 1000; // Количество шагов в секунду
|
||||
|
||||
void setup_wifi() {
|
||||
delay(10);
|
||||
// Подключение к WiFi
|
||||
WiFi.begin(ssid, password);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
}
|
||||
}
|
||||
|
||||
void startMotor() {
|
||||
motor_running = true;
|
||||
Serial.println("Motor started");
|
||||
}
|
||||
|
||||
void stopMotor() {
|
||||
motor_running = false;
|
||||
Serial.println("Motor stopped");
|
||||
}
|
||||
|
||||
void updateStepDelay() {
|
||||
// Рассчитываем задержку между шагами
|
||||
step_delay = 1000000 / steps_per_second;
|
||||
}
|
||||
|
||||
void setSpeed(int speed) {
|
||||
// Установка скорости в шагах в секунду
|
||||
if (speed > 0) {
|
||||
steps_per_second = speed;
|
||||
updateStepDelay();
|
||||
Serial.print("Speed set to: ");
|
||||
Serial.print(speed);
|
||||
Serial.println(" steps/sec");
|
||||
}
|
||||
}
|
||||
|
||||
void callback(char* topic, byte* payload, unsigned int length) {
|
||||
// Обработка команд через MQTT
|
||||
String message;
|
||||
for (int i = 0; i < length; i++) {
|
||||
message += (char)payload[i];
|
||||
}
|
||||
|
||||
if (message == "start") {
|
||||
startMotor();
|
||||
} else if (message == "stop") {
|
||||
stopMotor();
|
||||
} else if (message.startsWith("speed:")) {
|
||||
int speed = message.substring(6).toInt();
|
||||
setSpeed(speed);
|
||||
} else if (message.startsWith("turnoff")) {
|
||||
digitalWrite(EN_PIN, HIGH);
|
||||
} else if (message.startsWith("turnon")) {
|
||||
digitalWrite(EN_PIN, LOW);
|
||||
}
|
||||
}
|
||||
|
||||
void reconnect() {
|
||||
// Повторное подключение к MQTT
|
||||
while (!client.connected()) {
|
||||
if (client.connect("ESP32Client", mqtt_user, mqtt_password)) {
|
||||
client.subscribe("motor/control");
|
||||
} else {
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
setup_wifi();
|
||||
client.setServer(mqtt_server, 1883);
|
||||
client.setCallback(callback);
|
||||
|
||||
// Настройка пинов
|
||||
pinMode(DIR_PIN, OUTPUT);
|
||||
pinMode(STEP_PIN, OUTPUT);
|
||||
pinMode(EN_PIN, OUTPUT);
|
||||
|
||||
// Включаем двигатель
|
||||
digitalWrite(EN_PIN, LOW);
|
||||
|
||||
// Начальное направление (вперед)
|
||||
digitalWrite(DIR_PIN, HIGH);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (!client.connected()) {
|
||||
reconnect();
|
||||
}
|
||||
client.loop();
|
||||
// Обновляем задержку для правильной скорости
|
||||
updateStepDelay();
|
||||
// Управление двигателем
|
||||
if (motor_running) {
|
||||
// Выполняем шаг
|
||||
digitalWrite(STEP_PIN, HIGH);
|
||||
delayMicroseconds(step_delay);
|
||||
digitalWrite(STEP_PIN, LOW);
|
||||
delayMicroseconds(step_delay);
|
||||
}
|
||||
}
|
||||
|
||||
11
arduino_code/Test/test/README
Normal file
11
arduino_code/Test/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