Skip to content

Commit d7ef788

Browse files
authored
Add files via upload
1 parent f7e4ccb commit d7ef788

File tree

2 files changed

+103
-31
lines changed

2 files changed

+103
-31
lines changed

Test1.ino

Lines changed: 71 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,79 @@
1-
/*
2-
Arduino Brushless Motor Control
3-
by Dejan, https://howtomechatronics.com
4-
*/
51
#include <Servo.h>
6-
Servo ESC; // create servo object to control the ESC
7-
String x;
8-
int y;
2+
3+
Servo x1;
4+
Servo x2;
5+
Servo y1;
6+
Servo y2;
7+
Servo z0;
8+
9+
const byte numChars = 32;
10+
char receivedChars[numChars]; // an array to store the received data
11+
int x; // forward/back axis
12+
int y; // left/right axis
13+
int z; // Up/down Axis
14+
15+
boolean newData = false;
16+
boolean parsedData = false;
17+
918
void setup() {
10-
Serial.begin(9600);
11-
// Attach the ESC on pin 9
12-
ESC.attach(9,1000,2000); // (pin, min pulse width, max pulse width in microseconds)
19+
Serial.begin(9600);
20+
Serial.println("<Arduino is ready>");
21+
x1.attach(8,1000,2000);
22+
x2.attach(9,1000,2000);
23+
z0.attach(10,1000,2000);
24+
y1.attach(11,1000,2000);
25+
y2.attach(12,1000,2000);
26+
1327
}
28+
1429
void loop() {
15-
if(Serial.available()){
16-
x = Serial.readString();
17-
Serial.println(x);
30+
recvWithEndMarker();
31+
parseRX();
32+
updateXZProp();
33+
}
34+
35+
void recvWithEndMarker() {
36+
static byte ndx = 0;
37+
char endMarker = '\n';
38+
char rc;
1839

19-
y = x.toInt();
20-
ESC.write(y);
40+
while (Serial.available() > 0 && newData == false) {
41+
rc = Serial.read();
42+
43+
if (rc != endMarker) {
44+
receivedChars[ndx] = rc;
45+
ndx++;
46+
if (ndx >= numChars) {
47+
ndx = numChars - 1;
48+
}
49+
}
50+
else {
51+
receivedChars[ndx] = '\0'; // terminate the string
52+
ndx = 0;
53+
newData = true;
54+
}
55+
}
56+
}
57+
58+
void parseRX() {
59+
if (newData == true) {
60+
sscanf(receivedChars,"%*c %d %*c %d %*c %d",&x,&y,&z);
61+
newData = false;
62+
parsedData = true;
2163
}
2264
}
2365

24-
66+
void updateXZProp() {
67+
if (parsedData == true) {
68+
int xRX = x;
69+
int yRX = y;
70+
int zRX = z;
71+
Serial.println(x);
72+
Serial.println(y);
73+
Serial.println(z);
74+
x1.write(xRX);
75+
x2.write(xRX);
76+
z0.write(zRX);
77+
parsedData = false;
78+
}
79+
}

run.py

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,43 @@
11
from flask import Flask, request, render_template
22
import serial
3-
import time
43

54
app = Flask(__name__)
65

7-
port = serial.Serial(port= 'COM12', baudrate=9600)
6+
port = serial.Serial(port= 'COM11', baudrate=9600)
87
port.flushInput()
98
text = b'0'
109

11-
@app.route('/')
12-
def my_form():
13-
port.write(text)
14-
return render_template('my-form.html')
10+
# @app.route('/loading')
11+
# def loading():
12+
# rcvinit = ""
13+
# initline = "<Arduino is ready>"
14+
# ohgodohfuckmyairpods = 0
15+
# while ohgodohfuckmyairpods == 0:
16+
# rcvinit = port.readline()
17+
# if rcvinit == initline:
18+
# ohgodohfuckmyairpods += 1
19+
# return rcvinit
20+
# else:
21+
# return rcvinit
1522

1623

17-
@app.route('/', methods=['POST'])
18-
def my_form_post():
19-
text = request.form['text']
20-
port.write(text)
21-
return text
22-
23-
time.sleep(5)
24+
@app.route('/write', methods=['GET','POST'])
25+
def writeserial():
26+
if request.method == 'POST':
27+
text = request.form['text']
28+
text = text.encode()
29+
port.write(text)
30+
port.write(b'\n\r')
31+
return render_template('my-form.html')
32+
else:
33+
# port.write(b'0\n')
34+
return render_template('my-form.html')
2435

25-
26-
36+
# @app.route('/read')
37+
# def readserial():
38+
# port.read()
39+
# roombaFarts =
40+
# templateData = {
41+
# 'roombafarts': roombaFarts
42+
# }
43+
# return render_template('readserial.html', **templateData)

0 commit comments

Comments
 (0)