Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加旋转特定角度功能,同时指定espressif32版本号以修复烧录程序后页面无法打开的问题 #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions nano_core/include/speed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <stdint.h>
#include <functional>
#include <Ticker.h>

enum Direction { POS = 0, NEG = 1, STOP = 2 };

Expand Down Expand Up @@ -55,6 +56,9 @@ class SpeedManager {

bool set_web_speed(const Speed& speed);
bool set_web_json_speed(const char* str, int len);
bool set_web_max_speed(Direction dir);
void set_web_stop_speed();
void start_ticker(int duration_ms);

void need_update();

Expand All @@ -63,6 +67,7 @@ class SpeedManager {
private:
Speed get_speed(int id);
Speed max_speed(Direction dir);
Ticker ticker_;

static const double speed_tab[];

Expand Down
1 change: 1 addition & 0 deletions nano_core/include/web_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class NanoServer {
void handle_speed_set();
void handle_config_query();
void handle_config_set();
void handle_spin_certain_degree();
};

#endif // _WEBSERVER_HPP
16 changes: 16 additions & 0 deletions nano_core/src/speed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ bool RunningStatus::to_json(char* str,

const double SpeedManager::speed_tab[] = {0, 0, 10, 30, 0.5, 50, 100, 1};

void ticker_callback(SpeedManager* manager) {
manager->set_web_stop_speed();
}

SpeedManager::SpeedManager(int p1, int p2, int p3, int p4)
: p1_(p1),
p2_(p2),
Expand Down Expand Up @@ -126,6 +130,18 @@ bool SpeedManager::set_web_speed(const Speed& speed) {
return true;
}

bool SpeedManager::set_web_max_speed(Direction dir) {
return set_web_speed(max_speed(dir));
}

void SpeedManager::set_web_stop_speed() {
set_web_speed(Speed(Direction::STOP, SpeedType::DEGREE, 0));
}

void SpeedManager::start_ticker(int duration_ms) {
ticker_.once_ms(duration_ms, ticker_callback, this);
}

bool SpeedManager::set_web_json_speed(const char* str, int len) {
return web_speed_.from_json(str, len);
}
Expand Down
34 changes: 33 additions & 1 deletion nano_core/src/web_server.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "web_server.hpp"
#include <ArduinoJson.h>

#include "web_server.hpp"
#include "configure.hpp"
#include "speed.hpp"

bool NanoServer::init(fs::FS& fs, SpeedManager* manager) {
manager_ = manager;
Expand All @@ -17,6 +19,8 @@ bool NanoServer::init(fs::FS& fs, SpeedManager* manager) {
server_.on("/config", HTTP_POST, [this]() { this->handle_config_query(); });
server_.on("/set_config", HTTP_POST,
[this]() { this->handle_config_set(); });
server_.on("/spin", HTTP_POST,
[this]() { this->handle_spin_certain_degree(); });
return true;
}

Expand Down Expand Up @@ -65,6 +69,34 @@ void NanoServer::handle_speed_set() {
handle_status_query();
}

void NanoServer::handle_spin_certain_degree() {
String arg = server_.arg(0);
StaticJsonDocument<128> doc;
auto err = deserializeJson(doc, arg.c_str());
if (err) {
Serial.println(err.c_str());
server_.send(500, "text/json", R"({"ret": false})");
}
double deg = doc["deg"];

// Only allow positive degree to simplify logic
if (!(deg >= 0 && deg < 360)) {
server_.send(500, "text/json", R"({"ret": false})");
}
int tmp = doc["d"];
Direction d = (deg == 0) ? Direction::STOP : Direction(tmp);

double stepper_step =
MOTOR_STEP_DEG / (MOTOR_STEP_NUM * Config::inst().data.gearbox_ratio);
double duration_ms = deg / (1000 / double(MOTOR_MIN_DELAY_US) * stepper_step);
if (!manager_->set_web_max_speed(d)) {
server_.send(500, "text/json", R"({"ret": false})");
}
manager_->start_ticker(duration_ms);
manager_->need_update();
handle_config_query();
}

void NanoServer::handle_config_query() {
char buf[CONFIG_STR_LEN];
int len;
Expand Down
48 changes: 48 additions & 0 deletions nano_view/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class App extends React.Component {
// user input
input_speed: this.default_speed_list[0],
input_direction: this.default_direction_list[0],
input_degree: 0,
btn_touched: false,
// config info
config_fetched: false,
Expand Down Expand Up @@ -204,6 +205,21 @@ class App extends React.Component {
}).then(data => this.update_status(data));
}

set_spin_degree(dir, deg) {
return fetch("/spin", {
method: "POST",
body: JSON.stringify({
d: dir,
deg: deg
})
}).then(function (response) {
if (response.ok) {
return response.json();
}
throw new Error('error');
}).then(data => this.update_status(data));
}

set_config() {
fetch("/set_config", {
method: "POST",
Expand Down Expand Up @@ -305,6 +321,15 @@ class App extends React.Component {
}
}

handle_degree_input(event) {
let v = this.degree_control.value;
if (v === "") {
this.setState({input_degree: 0});
} else {
this.setState({input_degree: parseInt(v)});
}
}

handle_speed_radio(event) {
let v = this.speed_check_control.value;
if (v === "") {
Expand All @@ -322,6 +347,12 @@ class App extends React.Component {
}
}

handle_degree_btn(event) {
if (this.state.input_degree !== 0) {
this.set_spin_degree(this.state.input_direction, this.state.input_degree).catch(err => this.failed_handle(err));
}
}

handle_btn_push(event) {
if (this.state.status_direction !== DIRECTION.STOP) {
return;
Expand Down Expand Up @@ -440,10 +471,27 @@ class App extends React.Component {
</div>
</div>
</div>
<div className="in-card-part">
<div>
<h4>旋转角度</h4>
<div key={`degree_input`} className="direction_input">
<Form.Check>
<Form.Check.Label>
<Form.Control ref={(el) => {
this.degree_control = el;
}} onChange={this.handle_degree_input.bind(this)} className={"in-check-control"} size="sm"
type="number"/> <span>°</span>
</Form.Check.Label>
</Form.Check>
</div>
</div>
</div>
</div>
<div className="in-card-btn-group">
<Button
onClick={this.handle_start_btn.bind(this)}>{this.state.status_direction === DIRECTION.STOP ? "开始" : "停止"}</Button>
<Button
onClick={this.handle_degree_btn.bind(this)}>旋转</Button>
<Button disabled={this.state.status_direction !== DIRECTION.STOP && !this.state.btn_touched}
onTouchStart={this.handle_btn_push.bind(this)}
onMouseDown={this.handle_btn_push.bind(this)}
Expand Down