Skip to content

Commit 9b18783

Browse files
aoki-rtYusukeKato
andauthored
webserverとバッテリ残量通知の機能追加、リファクタリング (#10)
* update * change format * change format * change format * CIのコンパイルチェックにおける依存パッケージを追加 --------- Co-authored-by: YusukeKato <[email protected]>
1 parent cc50206 commit 9b18783

40 files changed

+2434
-1377
lines changed

.github/workflows/compile-sketches.yaml

+6-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ jobs:
2828
platforms: | # ESP32公式のpackage indexを使用する
2929
- name: esp32:esp32
3030
source-url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
31-
version: 3.0.1
31+
version: 3.1.1
32+
libraries: | # 依存パッケージを指定
33+
- name: AsyncTCP
34+
source-url: https://github.com/ESP32Async/AsyncTCP.git
35+
- name: ESPAsyncWebServer
36+
source-url: https://github.com/ESP32Async/ESPAsyncWebServer.git
3237
sketch-paths: |
3338
- pico_v2_STEP1_LED
3439
- pico_v2_STEP2_SWITCH

.github/workflows/lint.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
runs-on: ubuntu-latest
1515
steps:
1616
- uses: actions/checkout@v4
17-
- uses: arduino/arduino-lint-action@v1
17+
- uses: arduino/arduino-lint-action@v2
1818
with:
1919
recursive: true
2020
compliance: specification

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
Pi:Co V2用のArduinoサンプルスケッチ集です。
99

1010
## 動作環境
11-
12-
- arduino-esp32 : v3.0.1
11+
- Arduino IDE v2.3.4
12+
- arduino-esp32 v3.1.1
13+
- Async TCP 3.3.6
14+
- ESP Async WebServer 3.7.2
1315

1416
## サンプルスケッチについて
1517

pico_v2_STEP2_SWITCH/pico_v2_STEP2_SWITCH.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void loop()
4747
}
4848
if (digitalRead(SW_L) == 0) {
4949
digitalWrite(LED2, (++g_state_l) & 0x01);
50-
digitalWrite(LED1, (g_state_l)&0x01);
50+
digitalWrite(LED1, g_state_l & 0x01);
5151
}
5252
delay(30);
5353
while (!(digitalRead(SW_L) && digitalRead(SW_R))) {

pico_v2_STEP3_Buzzer/pico_v2_STEP3_Buzzer.ino

+5-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
char g_mode;
3333

34-
void setLED(char data)
34+
void ledSet(char data)
3535
{
3636
if (data & 0x01) {
3737
digitalWrite(LED0, HIGH);
@@ -55,7 +55,7 @@ void setLED(char data)
5555
}
5656
}
5757

58-
void execByMode(char mode)
58+
void modeExec(char mode)
5959
{
6060
switch (mode) {
6161
case 1:
@@ -94,7 +94,7 @@ void setup()
9494
ledcWrite(BUZZER, 0);
9595

9696
g_mode = 1;
97-
setLED(g_mode);
97+
ledSet(g_mode);
9898
}
9999

100100
void loop()
@@ -112,7 +112,7 @@ void loop()
112112
delay(30);
113113
ledcWrite(BUZZER, 0);
114114
}
115-
setLED(g_mode);
115+
ledSet(g_mode);
116116
}
117117
if (digitalRead(SW_L) == 0) {
118118
ledcWriteTone(BUZZER, INC_FREQ);
@@ -121,7 +121,7 @@ void loop()
121121
delay(80);
122122
ledcWrite(BUZZER, 0);
123123
delay(300);
124-
execByMode(g_mode);
124+
modeExec(g_mode);
125125
}
126126
while (!(digitalRead(SW_L) & digitalRead(SW_R))) {
127127
continue;

pico_v2_STEP5_Straight/pico_v2_STEP5_Straight.ino

+7-11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#include "run.h"
16+
1517
#define LED0 42
1618
#define LED1 41
1719
#define LED2 15
@@ -37,17 +39,11 @@ hw_timer_t * g_timer3 = NULL;
3739

3840
portMUX_TYPE g_timer_mux = portMUX_INITIALIZER_UNLOCKED;
3941

42+
volatile bool g_motor_move = 0;
43+
volatile unsigned int g_step_r, g_step_l;
4044
unsigned short g_step_hz_r = MIN_HZ;
4145
unsigned short g_step_hz_l = MIN_HZ;
4246

43-
volatile unsigned int g_step_r, g_step_l;
44-
double g_max_speed;
45-
double g_min_speed;
46-
double g_accel = 0.0;
47-
volatile double g_speed = MIN_SPEED;
48-
49-
volatile bool g_motor_move = 0;
50-
5147
//割り込み
5248
//目標値の更新周期1kHz
5349
void IRAM_ATTR onTimer0(void)
@@ -138,12 +134,12 @@ void loop()
138134
digitalWrite(MOTOR_EN, HIGH);
139135
delay(1000);
140136
digitalWrite(LED0, HIGH);
141-
accelerate(45, 200);
137+
g_run.accelerate(45, 200);
142138
digitalWrite(LED1, HIGH);
143139
digitalWrite(LED2, HIGH);
144-
oneStep(90, 200);
140+
g_run.oneStep(90, 200);
145141
digitalWrite(LED3, HIGH);
146-
decelerate(45, 200);
142+
g_run.decelerate(45, 200);
147143
digitalWrite(LED0, LOW);
148144
digitalWrite(LED1, LOW);
149145
digitalWrite(LED2, LOW);

pico_v2_STEP5_Straight/run.h

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2023 RT Corporation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef SRC_RUN_H_
16+
#define SRC_RUN_H_
17+
18+
typedef enum { MOT_FORWARD = 1, MOT_BACK = 2 } t_CW_CCW;
19+
20+
class RUN
21+
{
22+
private:
23+
public:
24+
volatile double accel;
25+
volatile double speed;
26+
volatile double max_speed;
27+
volatile double min_speed;
28+
29+
RUN();
30+
void interrupt(void);
31+
void counterClear(void);
32+
void dirSet(t_CW_CCW dir_left, t_CW_CCW dir_right);
33+
void speedSet(double l_speed, double r_speed);
34+
void stepGet(void);
35+
void stop(void);
36+
void accelerate(int len, int finish_speed);
37+
void oneStep(int len, int init_speed);
38+
void decelerate(int len, int init_speed);
39+
40+
private:
41+
int step_lr_len, step_lr;
42+
};
43+
44+
extern RUN g_run;
45+
46+
#endif /* SRC_RUN_H_ */

pico_v2_STEP5_Straight/run.ino

+103-36
Original file line numberDiff line numberDiff line change
@@ -12,64 +12,131 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
void accelerate(int len, int tar_speed)
15+
RUN g_run;
16+
17+
RUN::RUN()
18+
{
19+
speed = 0.0;
20+
accel = 0.0;
21+
}
22+
23+
//割り込み
24+
void controlInterrupt(void) { g_run.interrupt(); }
25+
26+
void RUN::interrupt(void)
27+
{ //割り込み内からコール
28+
speed += accel;
29+
30+
if (speed > max_speed) {
31+
speed = max_speed;
32+
}
33+
if (speed < min_speed) {
34+
speed = min_speed;
35+
}
36+
37+
speedSet(speed, speed);
38+
}
39+
40+
void RUN::dirSet(t_CW_CCW dir_left, t_CW_CCW dir_right)
41+
{
42+
if (dir_left == MOT_FORWARD) {
43+
digitalWrite(CW_L, LOW);
44+
} else {
45+
digitalWrite(CW_L, HIGH);
46+
}
47+
if (dir_right == MOT_FORWARD) {
48+
digitalWrite(CW_R, HIGH);
49+
} else {
50+
digitalWrite(CW_R, LOW);
51+
}
52+
}
53+
54+
void RUN::counterClear(void) { g_step_r = g_step_l = 0; }
55+
56+
void RUN::speedSet(double l_speed, double r_speed)
57+
{
58+
g_step_hz_l = (int)(l_speed / PULSE);
59+
g_step_hz_r = (int)(r_speed / PULSE);
60+
}
61+
62+
void RUN::stepGet(void)
63+
{
64+
step_lr = g_step_r + g_step_l;
65+
step_lr_len = (int)((float)step_lr / 2.0 * PULSE);
66+
}
67+
68+
void RUN::stop(void) { g_motor_move = 0; }
69+
70+
void RUN::accelerate(int len, int finish_speed)
1671
{
1772
int obj_step;
18-
g_max_speed = tar_speed;
19-
g_accel = 1.5;
20-
g_step_r = g_step_l = 0;
21-
g_speed = g_min_speed = MIN_SPEED;
22-
g_step_hz_r = g_step_hz_l = (unsigned short)(g_speed / PULSE);
2373

74+
accel = 1.5;
75+
speed = min_speed = MIN_SPEED;
76+
max_speed = finish_speed;
77+
counterClear();
78+
speedSet(speed, speed);
79+
dirSet(MOT_FORWARD, MOT_FORWARD);
2480
obj_step = (int)((float)len * 2.0 / PULSE);
25-
digitalWrite(CW_R, HIGH);
26-
digitalWrite(CW_L, LOW);
2781
g_motor_move = 1;
2882

29-
while ((g_step_r + g_step_l) < obj_step) {
30-
continue;
83+
while (1) {
84+
stepGet();
85+
if (step_lr > obj_step) {
86+
break;
87+
}
3188
}
3289
}
3390

34-
void oneStep(int len, int tar_speed)
91+
void RUN::oneStep(int len, int init_speed)
3592
{
3693
int obj_step;
37-
g_max_speed = tar_speed;
38-
g_accel = 0.0;
39-
g_step_r = g_step_l = 0;
40-
g_speed = g_min_speed = tar_speed;
41-
g_step_hz_r = g_step_hz_l = (unsigned short)(g_speed / PULSE);
94+
95+
accel = 0.0;
96+
max_speed = init_speed;
97+
speed = min_speed = init_speed;
98+
counterClear();
99+
speedSet(init_speed, init_speed);
100+
dirSet(MOT_FORWARD, MOT_FORWARD);
42101
obj_step = (int)((float)len * 2.0 / PULSE);
43-
digitalWrite(CW_R, HIGH);
44-
digitalWrite(CW_L, LOW);
45102

46-
while ((g_step_r + g_step_l) < obj_step) {
47-
continue;
103+
while (1) {
104+
stepGet();
105+
if (step_lr > obj_step) {
106+
break;
107+
}
48108
}
49109
}
50110

51-
void decelerate(int len, int tar_speed)
111+
void RUN::decelerate(int len, int init_speed)
52112
{
53113
int obj_step;
54-
g_max_speed = tar_speed;
55-
g_accel = 0.0;
56-
g_step_r = g_step_l = 0;
57-
g_speed = g_min_speed = tar_speed;
58-
g_step_hz_r = g_step_hz_l = (unsigned short)(g_speed / PULSE);
114+
115+
accel = 1.5;
116+
max_speed = init_speed;
117+
speed = min_speed = init_speed;
118+
counterClear();
119+
speedSet(init_speed, init_speed);
120+
dirSet(MOT_FORWARD, MOT_FORWARD);
59121
obj_step = (int)((float)len * 2.0 / PULSE);
60-
digitalWrite(CW_R, HIGH);
61-
digitalWrite(CW_L, LOW);
62122

63-
while ((len - (g_step_r + g_step_l) / 2.0 * PULSE) >
64-
(((g_speed * g_speed) - (MIN_SPEED * MIN_SPEED)) / (2.0 * 1000.0 * 1.5))) {
65-
continue;
123+
while (1) {
124+
stepGet();
125+
if (
126+
(len - step_lr_len) <
127+
(int)(((speed * speed) - (MIN_SPEED * MIN_SPEED)) / (2.0 * 1000.0 * accel))) {
128+
break;
129+
}
66130
}
67-
g_accel = -1.5;
68-
g_min_speed = MIN_SPEED;
131+
accel = -1.5;
132+
min_speed = MIN_SPEED;
69133

70-
while ((g_step_r + g_step_l) < obj_step) {
71-
continue;
134+
while (1) {
135+
stepGet();
136+
if (step_lr > obj_step) {
137+
break;
138+
}
72139
}
73140

74-
g_motor_move = 0;
141+
stop();
75142
}

0 commit comments

Comments
 (0)