From 857047683bae4dce724370ee6d835bfc60bfcd9d Mon Sep 17 00:00:00 2001 From: Cynopolis Date: Fri, 20 Mar 2020 11:55:04 -0500 Subject: [PATCH] Backup before switching to flexx for GUI Added serial communication class that can succesfully communicate with the arduino. Started work on pygame GUI but I've decided to switch direction to using Flexx instead. --- BleedingHeart/BleedingHeart.ino | 7 ++- Interface/Button.py | 34 +++++++++++ Interface/Comm.py | 60 ++++++++++++++++++++ Interface/__pycache__/Button.cpython-36.pyc | Bin 0 -> 1441 bytes Interface/main.py | 38 +++---------- 5 files changed, 107 insertions(+), 32 deletions(-) create mode 100644 Interface/Button.py create mode 100644 Interface/Comm.py create mode 100644 Interface/__pycache__/Button.cpython-36.pyc diff --git a/BleedingHeart/BleedingHeart.ino b/BleedingHeart/BleedingHeart.ino index e5be007..e26769c 100644 --- a/BleedingHeart/BleedingHeart.ino +++ b/BleedingHeart/BleedingHeart.ino @@ -72,15 +72,20 @@ void processString(){ Serial.print("Heart Rate set to "); Serial.print(heartRate); Serial.println(" BPM."); + delay(500); } else if (data.substring(0, 5) == "PRSR "){ targetPressure = data.substring(5, data.length()).toDouble(); Serial.print("Target Pressure set to "); Serial.print(targetPressure); Serial.println(" PSI."); + delay(500); } else if (data.substring(0, 5) == "FILL "){ int fillTime = data.substring(5, data.length()).toInt(); + Serial.print("Filling for "); + Serial.print(fillTime); + Serial.println(" seconds."); digitalWrite(valvePin1, HIGH); digitalWrite(valvePin2, HIGH); analogWrite(motorPin, 0); @@ -114,7 +119,7 @@ void printPID(){ Serial.print(pressure); Serial.print(", Target Pressure: "); Serial.print(targetPressure); - Serial.print(", Motor Speed:"); + Serial.print(", Motor Speed: "); Serial.println(motorSpeed); } diff --git a/Interface/Button.py b/Interface/Button.py new file mode 100644 index 0000000..d3f9686 --- /dev/null +++ b/Interface/Button.py @@ -0,0 +1,34 @@ +import pygame +class Button: + pos = [0,0] + size = [0,0] + color = (0, 80, 80) + def __init__(self, position, size, screen, function, argument): + self.pos = position + self.size = size + self.function = function + self.argument = argument + self.screen = screen + return + + def isClicked(self, mouse_x, mouse_y, mouse_state): + x, y = mouse_x, mouse_y + if x >= self.pos[0] and x <= self.pos[0]+self.size[0]: + if y >= self.pos[1] and y <= self.pos[1]+self.size[1]: + if mouse_state == True: + self.function(self.argument) + return True + return False + + def setPosition(self, x, y): + self.pos[0], self.pos[1] = x, y + return + + def setArgument(self, argument): + self.argument = argument + return + + def drawButton(self): + pygame.draw.rect(self.screen, self.color, + pygame.Rect(self.pos[0], self.pos[1], self.size[0], self.size[1])) + return \ No newline at end of file diff --git a/Interface/Comm.py b/Interface/Comm.py new file mode 100644 index 0000000..b3642bd --- /dev/null +++ b/Interface/Comm.py @@ -0,0 +1,60 @@ +import serial +from time import sleep + +class Comm: + current_pressure = -1 + target_pressure = -1 + motor_speed = -1 + def __init__(self, port, baud_rate): + # Setup serial + self.comm = serial.Serial(port, baud_rate) + #read a line from the serial bus to sync arduino with python script + for x in range(0,5): + self.comm.readline() + self.getPID() + return + + def getPID(self): + message = self.comm.readline().decode().strip() + #message = 'Current Pressure: 80, Target Pressure: 15, Motor Speed: 255' + #print(message) + if message[0:7] == 'Current': + startIndex = message.find(':') + endIndex = message.find(',') + self.current_pressure = float(message[startIndex+1:endIndex]) + startIndex = message.find(':', endIndex) + endIndex = message.find(',', endIndex+1) + self.target_pressure = float(message[startIndex+1:endIndex]) + startIndex = message.find(':', endIndex) + self.motor_speed = float(message[startIndex+1:len(message)]) + #print("1", self.current_pressure, "2", self.target_pressure, "3", self.motor_speed) + return (self.current_pressure, self.target_pressure, self.motor_speed) + else: + return (self.current_pressure, self.target_pressure, self.motor_speed) + + def setPressure(self, pressure): + pressure = float(pressure) + msg = 'PRSR ' + str(pressure) + '\r\n' + while self.getPID()[1] != pressure: + self.comm.write(msg.encode()) + return + + def setHeartRate(self, BPM): + msg = 'BEAT ' + str(BPM) + '\r\n' + response = 'Heart Rate set to ' + str(BPM) + ' BPM.' + ans = '' + while ans != response: + self.comm.write(msg.encode()) + ans = self.comm.readline().decode().strip() + return + + def fill(self, time): + msg = 'FILL ' + str(time) + '\r\n' + response = 'Filling for ' + str(time) + ' seconds.' + ans = '' + while ans != response: + self.comm.write(msg.encode()) + ans = self.comm.readline().decode().strip() + return + +#comm = Comm('COM11', 115200) \ No newline at end of file diff --git a/Interface/__pycache__/Button.cpython-36.pyc b/Interface/__pycache__/Button.cpython-36.pyc new file mode 100644 index 0000000000000000000000000000000000000000..ea41ed636aeff54ab0906c06734f736e828ce677 GIT binary patch literal 1441 zcmZuw%Z}496txp4b=!G^Km!X93#1^#h8?O9%phKi(eT<-6;X*jB{OX@*r}MQ%4+x- ze#u)_tl6>RTqo@e4@WuIj}u?}o|F5ltDT>7^Zu@2>^r;ALi8WuwbuZODL!U>_Kc}O zg}3FY`tMFSQ%vWjp)?(1u^KLAVI*s zh%XV9;}C*wv!loq=OfHQqHT7S9dXQ~TwnzaYdP{P%r>l0g+~F{>=30s49_@+Io>cO zP~d2t05U!ou z-Ep2BX|;x-C1AY6Z{v}Cg$q90Xv(uBW@Jrj6qofOi`Vt>cji6jW6*=~agVURhoJ-s z_qRwwe{8dkDYHR9%3nBJ*XnA&@yb;!SuYH~!e?8}nJy4AvMx1>OB;7EsBOHkEB7`_ zf;u-@?tf_?H|D*L^(JlnN7@A;BVnjfBo(|_dgxTJLMmeQ9(uR`%2Df-4 Ao&W#< literal 0 HcmV?d00001 diff --git a/Interface/main.py b/Interface/main.py index e4eaa77..94ef65c 100644 --- a/Interface/main.py +++ b/Interface/main.py @@ -1,5 +1,6 @@ import pygame -import serial +from Button import Button +from Comm import Comm def createText(font, text, position): text = font.render(text, True, (0,0,0), (255,255,255)) @@ -23,14 +24,17 @@ texts.append(createText(font, 'Pressure (PSI)', positions[0])) texts.append(createText(font, 'Heart Rate (BPM)', positions[1])) texts.append(createText(font, '0', positions[2])) texts.append(createText(font, '0', positions[3])) -def test(nothing): + +comm = Comm('COM10', 115200) +def test(): return -testButton = Button([10,10], [50,50], pygame, test, "Hello") +testButton = Button([10,10], [50,50], screen, test, "Hello") def main(): done = False while not done: screen.fill((255, 255, 255)) + testButton.drawButton() for text in texts: screen.blit(text[0], text[1]) for event in pygame.event.get(): @@ -42,33 +46,5 @@ def main(): pygame.quit() return -class Button: - pos = [0,0] - size = [0,0] - def __init__(self, position, size, pygame, function, argument): - self.pos = position - self.size = size - self.function = function - self.argument = argument - self.pygame = pygame - return - - def isClicked(self): - x, y = self.pygame.get_pos()[0], self.pygame.get_pos()[1] - if x >= self.pos[0] and x <= self.pos[0]+self.size[0]: - if y >= self.pos[1] and y <= self.pos[1]+self.size[1]: - if pygame.mouse.get_pressed()[0] == True: - self.function(self.argument) - return True - return False - - def setPosition(self, x, y): - self.pos[0], self.pos[1] = x, y - return - - def setArgument(self, argument): - self.argument = argument - return - if __name__ == "__main__": main()