add ultrasonic
This commit is contained in:
parent
cb4d09b426
commit
7a4e9df6ce
@ -6,11 +6,13 @@
|
|||||||
#include "robot_state.h"
|
#include "robot_state.h"
|
||||||
#include "ws_server.h"
|
#include "ws_server.h"
|
||||||
#include "servo.h"
|
#include "servo.h"
|
||||||
|
#include "ultrasonic.h"
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
|
||||||
servoInit();
|
servoInit();
|
||||||
|
ultrasonicInit();
|
||||||
actuatorsInit();
|
actuatorsInit();
|
||||||
webServerInit();
|
webServerInit();
|
||||||
wsInit();
|
wsInit();
|
||||||
|
|||||||
38
src/ultrasonic.cpp
Normal file
38
src/ultrasonic.cpp
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
#include <Arduino.h>
|
||||||
|
#include "ultrasonic.h"
|
||||||
|
|
||||||
|
// ===== PIN CONFIG =====
|
||||||
|
#define TRIG_PIN 32
|
||||||
|
#define ECHO_PIN 33
|
||||||
|
|
||||||
|
// таймаут эха (30 мс ≈ 5 м)
|
||||||
|
static const uint32_t ECHO_TIMEOUT_US = 30000;
|
||||||
|
|
||||||
|
void ultrasonicInit() {
|
||||||
|
pinMode(TRIG_PIN, OUTPUT);
|
||||||
|
pinMode(ECHO_PIN, INPUT);
|
||||||
|
|
||||||
|
digitalWrite(TRIG_PIN, LOW);
|
||||||
|
}
|
||||||
|
|
||||||
|
// измерение расстояния
|
||||||
|
int ultrasonicReadCm() {
|
||||||
|
// триггер
|
||||||
|
digitalWrite(TRIG_PIN, LOW);
|
||||||
|
delayMicroseconds(2);
|
||||||
|
|
||||||
|
digitalWrite(TRIG_PIN, HIGH);
|
||||||
|
delayMicroseconds(10);
|
||||||
|
digitalWrite(TRIG_PIN, LOW);
|
||||||
|
|
||||||
|
// читаем эхо
|
||||||
|
uint32_t duration = pulseIn(ECHO_PIN, HIGH, ECHO_TIMEOUT_US);
|
||||||
|
|
||||||
|
if (duration == 0) {
|
||||||
|
return -1; // нет эха
|
||||||
|
}
|
||||||
|
|
||||||
|
// скорость звука ≈ 343 м/с
|
||||||
|
int distance = duration / 58;
|
||||||
|
return distance;
|
||||||
|
}
|
||||||
7
src/ultrasonic.h
Normal file
7
src/ultrasonic.h
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
void ultrasonicInit();
|
||||||
|
|
||||||
|
// возвращает расстояние в см
|
||||||
|
// -1 = нет эхо / ошибка
|
||||||
|
int ultrasonicReadCm();
|
||||||
@ -5,6 +5,7 @@
|
|||||||
#include "web_ui.h"
|
#include "web_ui.h"
|
||||||
#include "controller.h"
|
#include "controller.h"
|
||||||
#include "robot_state.h"
|
#include "robot_state.h"
|
||||||
|
#include "ultrasonic.h"
|
||||||
|
|
||||||
static WebServer server(80);
|
static WebServer server(80);
|
||||||
|
|
||||||
@ -68,13 +69,16 @@ void handleMode() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void handleStatus() {
|
void handleStatus() {
|
||||||
|
int dist = ultrasonicReadCm();
|
||||||
|
|
||||||
String json = "{";
|
String json = "{";
|
||||||
|
|
||||||
json += "\"mode\":\"" + String(modeToStr(robot.mode)) + "\",";
|
json += "\"mode\":\"" + String(modeToStr(robot.mode)) + "\",";
|
||||||
json += "\"speedL\":" + String(robot.speedL) + ",";
|
json += "\"speedL\":" + String(robot.speedL) + ",";
|
||||||
json += "\"speedR\":" + String(robot.speedR) + ",";
|
json += "\"speedR\":" + String(robot.speedR) + ",";
|
||||||
json += "\"rssi\":" + String(WiFi.RSSI()) + ",";
|
json += "\"rssi\":" + String(WiFi.RSSI()) + ",";
|
||||||
json += "\"uptime\":" + String(millis());
|
json += "\"uptime\":" + String(millis()) + ",";
|
||||||
|
json += "\"distance_cm\":" + String(dist);
|
||||||
|
|
||||||
json += "}";
|
json += "}";
|
||||||
|
|
||||||
|
|||||||
@ -125,13 +125,14 @@ function wsConnect(){
|
|||||||
`MODE=${j.mode} | ` +
|
`MODE=${j.mode} | ` +
|
||||||
`L=${j.speedL} R=${j.speedR} | ` +
|
`L=${j.speedL} R=${j.speedR} | ` +
|
||||||
`RSSI=${j.rssi}dBm | ` +
|
`RSSI=${j.rssi}dBm | ` +
|
||||||
|
`DIST=${j.distance_cm < 0 ? '---' : j.distance_cm + 'cm'} | ` +
|
||||||
`UP=${Math.floor(j.uptime/1000)}s`
|
`UP=${Math.floor(j.uptime/1000)}s`
|
||||||
);
|
);
|
||||||
}catch(e){
|
}catch(e){
|
||||||
st('STATUS ERR');
|
st('STATUS ERR');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
setInterval(updateStatus, 500);
|
setInterval(updateStatus, 5000);
|
||||||
|
|
||||||
// управление "пока держишь" (heartbeat)
|
// управление "пока держишь" (heartbeat)
|
||||||
function bindHold(btn, onPressCmd){
|
function bindHold(btn, onPressCmd){
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user