|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import Jetson.GPIO as GPIO |
| 4 | +import time |
| 5 | +import serial |
| 6 | + |
| 7 | +right_motor_pins = [ |
| 8 | + (26, 20), |
| 9 | + (12, 6), |
| 10 | + (17, 18) |
| 11 | +] |
| 12 | + |
| 13 | +left_motor_pins = [ |
| 14 | + (16, 19), |
| 15 | + (8, 11), |
| 16 | + (25, 9) |
| 17 | +] |
| 18 | + |
| 19 | +GPIO.setmode(GPIO.BCM) |
| 20 | +GPIO.setwarnings(False) |
| 21 | +ser = serial.Serial('/dev/ttyUSB0', 57600, timeout=1) |
| 22 | + |
| 23 | +for pin_pair in left_motor_pins + right_motor_pins: |
| 24 | + GPIO.setup(pin_pair[0], GPIO.OUT) |
| 25 | + GPIO.setup(pin_pair[1], GPIO.OUT) |
| 26 | + |
| 27 | +def move_forward(): |
| 28 | + for pin1, pin2 in left_motor_pins + right_motor_pins: |
| 29 | + GPIO.output(pin1, GPIO.HIGH) |
| 30 | + GPIO.output(pin2, GPIO.LOW) |
| 31 | + |
| 32 | +def move_backward(): |
| 33 | + for pin1, pin2 in left_motor_pins + right_motor_pins: |
| 34 | + GPIO.output(pin2, GPIO.HIGH) |
| 35 | + GPIO.output(pin1, GPIO.LOW) |
| 36 | + |
| 37 | +def stop(): |
| 38 | + for pin1, pin2 in left_motor_pins + right_motor_pins: |
| 39 | + GPIO.output(pin1, GPIO.LOW) |
| 40 | + GPIO.output(pin2, GPIO.LOW) |
| 41 | + |
| 42 | +def rotate(angle, direction='left', time_constant=0.02): |
| 43 | + rotation_time = angle * time_constant |
| 44 | + |
| 45 | + if direction == 'left': |
| 46 | + for pin1, pin2 in left_motor_pins: |
| 47 | + GPIO.output(pin1, GPIO.LOW) |
| 48 | + GPIO.output(pin2, GPIO.HIGH) |
| 49 | + |
| 50 | + for pin1, pin2 in right_motor_pins: |
| 51 | + GPIO.output(pin1, GPIO.HIGH) |
| 52 | + GPIO.output(pin2, GPIO.LOW) |
| 53 | + |
| 54 | + elif direction == 'right': |
| 55 | + for pin1, pin2 in left_motor_pins: |
| 56 | + GPIO.output(pin1, GPIO.HIGH) |
| 57 | + GPIO.output(pin2, GPIO.LOW) |
| 58 | + |
| 59 | + for pin1, pin2 in right_motor_pins: |
| 60 | + GPIO.output(pin1, GPIO.LOW) |
| 61 | + GPIO.output(pin2, GPIO.HIGH) |
| 62 | + |
| 63 | + time.sleep(rotation_time) |
| 64 | + stop() |
| 65 | + |
| 66 | +def receive_data(): |
| 67 | + if ser.in_waiting > 0: |
| 68 | + data = ser.readline().decode().strip() |
| 69 | + return data |
| 70 | + |
| 71 | +def mov(direction, t): |
| 72 | + try: |
| 73 | + t = float(t) # convert t to float in case of decimals |
| 74 | + if direction == 'w': |
| 75 | + move_forward() |
| 76 | + time.sleep(t) |
| 77 | + elif direction == 's': |
| 78 | + move_backward() |
| 79 | + time.sleep(t) |
| 80 | + elif direction == 'a': |
| 81 | + rotate(90, 'left') |
| 82 | + elif direction == 'd': |
| 83 | + rotate(90, 'right') |
| 84 | + elif direction == 'q': |
| 85 | + stop() |
| 86 | + elif direction == 'rl': |
| 87 | + rotate(t, 'left') |
| 88 | + elif direction == 'rr': |
| 89 | + rotate(t, 'right') |
| 90 | + stop() |
| 91 | + return |
| 92 | + else: |
| 93 | + print("Invalid command!") |
| 94 | + except ValueError: |
| 95 | + print("Error in time conversion.") |
| 96 | + stop() |
| 97 | +''' |
| 98 | +try: |
| 99 | + print('Manual Mode Started') |
| 100 | + while True: |
| 101 | + data = receive_data() |
| 102 | + if data: |
| 103 | + print(data) |
| 104 | + try: |
| 105 | + d, t = data.split(" ") |
| 106 | + mov(d, t) |
| 107 | + except ValueError: |
| 108 | + print("Received malformed data: ", data) |
| 109 | +finally: |
| 110 | + ser.close() |
| 111 | + GPIO.cleanup() |
| 112 | +''' |
| 113 | +rotate(90, 'left') |
| 114 | +move_forward() |
| 115 | +time.sleep(10) |
| 116 | +stop() |
0 commit comments