From ad16c4908ee4159f5ee24c2135c49f2d93f5da39 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Sun, 11 Dec 2022 21:34:17 +0700 Subject: [PATCH] Keyboard+motion+RobotLane --- KeyPressModule.py | 27 +++++++++++++++++++++++++++ MainRobotLane.py | 2 +- RobotMain2.py | 22 ++++++++++++++++++++++ kuukar/kuukar_cv.py | 2 +- kuukar/kuukar_motion.py | 4 ++-- 5 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 KeyPressModule.py create mode 100644 RobotMain2.py diff --git a/KeyPressModule.py b/KeyPressModule.py new file mode 100644 index 0000000..a1a40f3 --- /dev/null +++ b/KeyPressModule.py @@ -0,0 +1,27 @@ +import pygame + +def init(): + pygame.init() + win = pygame.display.set_mode((100,100)) + +def getKey(keyName): + ans = False + for eve in pygame.event.get():pass + keyInput = pygame.key.get_pressed() + myKey = getattr(pygame,'K_{}'.format(keyName)) + if keyInput [myKey]: + ans = True + pygame.display.update() + + return ans + +def main(): + if getKey('LEFT'): + print('Key Left was pressed') + if getKey('RIGHT'): + print('Key Right was pressed') + +if __name__ == '__main__': + init() + while True: + main() \ No newline at end of file diff --git a/MainRobotLane.py b/MainRobotLane.py index c9b6cf8..5f79a11 100644 --- a/MainRobotLane.py +++ b/MainRobotLane.py @@ -30,7 +30,7 @@ def main(): if curveVal>-0.08: curveVal=0 car.motion.skt_drive(0.4,-curveVal*sen,0.05) turn_value = -curveVal*sen - print("Curve Value : " + str(curveVal) + "Turn Value : "+ str(turn_value)) + print("Curve Value : " + str(curveVal) + " Turn Value : "+ str(round(turn_value,2)) cv2.waitKey(1) diff --git a/RobotMain2.py b/RobotMain2.py new file mode 100644 index 0000000..a93a1e7 --- /dev/null +++ b/RobotMain2.py @@ -0,0 +1,22 @@ +from kuukar.kuukar import kuukar +import KeyPressModule as kp + +car = kuukar() +kp.init() + +def main(self): + + if kp.getKey('UP'): + car.motion.skt_drive(0.4,0.0,0.05) + elif kp.getKey('DOWN'): + car.motion.skt_drive(-0.4,0.0,0.05) + elif kp.getKey('LEFT'): + car.motion.skt_drive(0.5,0.3,0.05) + elif kp.getKey('RIGHT'): + car.motion.skt_drive(0.5,-0.3,0.05) + else: + car.motion.stop() + +if __name__ == '__main__': + while True: + main() \ No newline at end of file diff --git a/kuukar/kuukar_cv.py b/kuukar/kuukar_cv.py index fc2e8cc..63b6cea 100644 --- a/kuukar/kuukar_cv.py +++ b/kuukar/kuukar_cv.py @@ -16,7 +16,7 @@ class cv: def _cv_loop(self): while True: img = self.capture_image() - stop_sign = self.detect_sign(img) + ##stop_sign = self.detect_sign(img) def get_road_curve(self, frame): pass diff --git a/kuukar/kuukar_motion.py b/kuukar/kuukar_motion.py index 7cffff4..444316a 100644 --- a/kuukar/kuukar_motion.py +++ b/kuukar/kuukar_motion.py @@ -132,8 +132,8 @@ class motion: self.motor_write(MOTOR_FL_F, MOTOR_FL_R, abs(leftSpeed)) self.motor_write(MOTOR_FR_F, MOTOR_FR_R, abs(rightSpeed)) - ##self.motor_write(MOTOR_RL_F, MOTOR_RL_R, speed) - ##self.motor_write(MOTOR_RR_F, MOTOR_RR_R, speed) + self.motor_write(MOTOR_RL_F, MOTOR_RL_R, speed) + self.motor_write(MOTOR_RR_F, MOTOR_RR_R, speed) time.sleep(t)