Software Project : Robotic Arm (4) - เขียน Web ครั้งที่ 2
ครั้งนี้เป็นการเขียนเเบบ กดปุ่มซ้าย-ขวา ขยับทีละ 5 องศา
โค้ด HTML
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="BEAUTIFUL.css">
</head>
<title>{{ Author }}'s app</title>
<body>
<h2> Servo </h2>
<form action="/" method="post">
<input type="submit" name="submit" id="keys1" value="1/L">
<input type="submit" name="submit" id="keys2" value="1/R"><p>
<input type="submit" name="submit" id="keys3" value="2/L">
<input type="submit" name="submit" id="keys4" value="2/R"><p>
<input type="submit" name="submit" id="keys5" value="3/L">
<input type="submit" name="submit" id="keys6" value="3/R"><p>
<input type="submit" name="submit" id="keys7" value="4/L">
<input type="submit" name="submit" id="keys8" value="4/R"><p>
<input type="submit" name="submit" id="keys9" value="5/L">
<input type="submit" name="submit" id="keys10" value="5/R"><p>
<input type="submit" name="submit" id="keys11" value="6/L">
<input type="submit" name="submit" id="keys12" value="6/R"><p>
</form>
</body>
</html>
โค้ด Python
import serial
import time
servo = serial.Serial("COM4", 9600)
class Servo():
def __init__(self,N):
self.V = N
def LA0(self):
servo.write(bytearray([self.V]))
time.sleep(0.05)
from flask import Flask,render_template,request
import time
N = 0
X = 37
Y = 74
Z = 111
O = 148
W = 185
app = Flask(__name__)#Webapp
time.sleep(3)#delay 3 วินาที
@app.route('/', methods=['POST', 'GET'])
def CS():
Author = "Gram"
if request.method == "POST":
import BA
global N,X,Y,Z,O,W
A = BA.Servo(N)
if request.form['submit']=='1/L':
if N <= 34:
N += 2
A.LA0()
print(N)
elif request.form['submit']=='1/R':
if N >= 2:
N -= 2
A.LA0()
print(N)
elif request.form['submit']=='2/L':
if X <= 73:
X -= 2
A.LA0()
print(X)
elif request.form['submit']=='2/R':
if X >= 39:
X -= 2
A.LA0()
print(X)
elif request.form['submit']=='3/L':
if Y <= 108:
Y -= 2
A.LA0()
print(Y)
elif request.form['submit']=='3/R':
if Y >= 76:
Y -= 2
A.LA0()
print(Y)
elif request.form['submit'] == '4/L':
if Z <= 108:
Z -= 2
A.LA0()
print(Z)
elif request.form['submit'] == '4/R':
if Z >= 76:
Z -= 2
A.LA0()
print(Z)
elif request.form['submit']=='5/L':
if O <= 108:
O -= 2
A.LA0()
print(O)
elif request.form['submit']=='5/R':
if O >= 76:
O -= 2
A.LA0()
print(O)
elif request.form['submit']=='6/L':
if W <= 108:
W -= 2
A.LA0()
print(W)
elif request.form['submit']=='6/R':
if W >= 76:
W -= 2
A.LA0()
print(W)
return render_template('index.html',Author=Author,value = 100)
if __name__ == "__main__":
app.run(debug=True)
จะได้หน้าเว็บดังรูป
โค้ดเเบบที่ 2
ข้อดี โค้ดเเบบที่ 2 คือ สั้นกว่าเเบบเเรก
ข้อเสีย จะสามารถขยับได้ทีละ 5 องศาเท่านั้น

ความคิดเห็น
แสดงความคิดเห็น