Files
BP_Gripper_Control_WS_23_24/controllServo.py
T
2024-01-24 20:37:25 +01:00

53 lines
1.1 KiB
Python

import serial
#pip3 install pyserial
MAX_SERV = 7
currentState = [1, 2, 3, 4, 5, 6, 7]
globalSerial = 0
def openSerial():
port = "/dev/ttyACM0"
baud = 9600
ser = serial.Serial(port, baud, timeout=1)
return ser
# Make sure data is in the format like: "%d,%d,%d,%d,%d,%d,%d"
def sendData(ser, data: str):
dataSend = "1" + data
ser.write(dataSend.encode("utf-8"))
print("=Data written=")
def buildAndSendData():
sendThis = ""
for x in range(0, MAX_SERV):
sendThis += str(currentState[x]) + ","
sendThis = sendThis[:-1]
print(sendThis)
sendData(globalSerial,sendThis)
def printState():
for x in range(0, MAX_SERV):
print("Servo {} is set to {}".format(x,currentState[x]))
def testLoop():
while True:
printState()
print("=========")
id = input("Type in an ID\n")
degree = input("Type in an Degree\n")
if id and degree:
currentState[int(id)] = int(degree)
buildAndSendData()
print(chr(27) + "[2J") #Clear terminal maybe
pass
if __name__ == "__main__":
globalSerial = openSerial()
buildAndSendData()
testLoop()