Skip to content

Commit

Permalink
formatting with black
Browse files Browse the repository at this point in the history
  • Loading branch information
BlakePR committed Mar 18, 2024
1 parent 32bf78d commit e8b1f1f
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 60 deletions.
21 changes: 11 additions & 10 deletions Arduino.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# CarControl.py
'''
"""
******* Controlling The Car Through Arduino **********
By DJ Lee on February 5, 2022
Expand All @@ -21,11 +21,12 @@
and don't go beyond that speed. These cars can go very fast, and there is expensive hardware
on them, so don't risk losing control of the car and breaking anything.
**************************************
'''
"""

import serial
import time


class Arduino:
def __init__(self, Port, Baud):
# Create serial port communication
Expand All @@ -41,31 +42,31 @@ def __init__(self, Port, Baud):
def __del__(self):
self.SerialPort.close()

def steer(self, degree): # control car steering -30.0 ~ 30.0 degrees
def steer(self, degree): # control car steering -30.0 ~ 30.0 degrees
command = "steer" + str(degree) + "\n"
self.SerialPort.write(command.encode())

def drive(self, speed): # control car speed -3.0 ~ 3.0 meters per secone
def drive(self, speed): # control car speed -3.0 ~ 3.0 meters per secone
command = "drive" + str(speed) + "\n"
self.SerialPort.write(command.encode())

def zero(self, pwm): # set PWM value when the car goes straight (0 degree)
def zero(self, pwm): # set PWM value when the car goes straight (0 degree)
command = "zero" + str(pwm) + "\n"
self.SerialPort.write(command.encode())

def encoder(self): # read encoder count back from Arduino
def encoder(self): # read encoder count back from Arduino
command = "encoder" + "\n"
self.SerialPort.writeexcept(command.encode())
return(self.SerialPort.readline())
return self.SerialPort.readline()

def pid(self, flag): # read encoder count back from Arduino
def pid(self, flag): # read encoder count back from Arduino
command = "pidtry:" + str(flag) + "\n"
self.SerialPort.write(command.encode())

def kp(self, p): # read encoder count back from Arduino
def kp(self, p): # read encoder count back from Arduino
command = "kp" + str(p) + "\n"
self.SerialPort.write(command.encode())

def kd(self, d): # read encoder count back from Arduino
def kd(self, d): # read encoder count back from Arduino
command = "kd" + str(d) + "\n"
self.SerialPort.write(command.encode())
24 changes: 15 additions & 9 deletions CameraTest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# $ python3 CameraTest.py
'''
"""
******* Realsense camera as the sensor ***************
The Intel Realsense 435i camera provides
RGB Data
Expand All @@ -13,13 +13,13 @@
'i' to save an image
'q' to quit the program
***********************************************
'''
"""
# import the necessary packages
from RealSense import *
import cv2
import imutils
import imutils

rs = RealSense("/dev/video2", RS_VGA) # RS_VGA, RS_720P, or RS_1080P
rs = RealSense("/dev/video2", RS_VGA) # RS_VGA, RS_720P, or RS_1080P
writer = None
recording = False
frameIndex = 0
Expand All @@ -28,21 +28,27 @@

if writer is None and recording is True:
# initialize our video writer
writer = cv2.VideoWriter('Video.avi', cv2.VideoWriter_fourcc(*'MJPG'), 15, (rgb.shape[1], rgb.shape[0]), True)
writer = cv2.VideoWriter(
"Video.avi",
cv2.VideoWriter_fourcc(*"MJPG"),
15,
(rgb.shape[1], rgb.shape[0]),
True,
)

cv2.imshow("RGB", rgb)
cv2.imshow("Depth", depth)

if recording == True:
# write the output frame to disk
writer.write(rgb)

key = cv2.waitKey(1) & 0xFF
if key == ord("r"): # Start Recording
if key == ord("r"): # Start Recording
recording = True
if key == ord("s"): # Stop Recording
if key == ord("s"): # Stop Recording
recording = False
if key == ord("i"): # Save image
if key == ord("i"): # Save image
filename = "image" + str(frameIndex) + ".jpg"
cv2.imwrite(filename, rgb)
frameIndex += 1
Expand Down
24 changes: 11 additions & 13 deletions CarTest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# $ python3 CarTest.py

'''
"""
******* Controlling The Car **********
By DJ Lee on February 5, 2022
Expand All @@ -22,33 +22,31 @@
and don't go beyond that speed. These cars can go very fast, and there is expensive hardware
on them, so don't risk losing control of the car and breaking anything.
**************************************
'''
"""

from Arduino import Arduino

# Use $ ls /dev/tty* to find the serial port connected to Arduino
Car = Arduino("/dev/ttyUSB0", 115200) # Linux
#Car = Arduino("/dev/tty.usbserial-2140", 115200) # Mac
Car = Arduino("/dev/ttyUSB0", 115200) # Linux
# Car = Arduino("/dev/tty.usbserial-2140", 115200) # Mac

while True:
command = input("Enter a command:\n")
if command == 's':
if command == "s":
angle = input("Enter a steering angle (-30 ~ 30):\n")
Car.steer(float(angle))
elif command == 'd':
elif command == "d":
speed = input("Enter a drive speed (-3.0 ~ 3.0):\n")
Car.drive(float(speed))
elif command == 'z':
elif command == "z":
pwm = input("Enter a PWM value (~1500):\n")
Car.zero(int(pwm))
elif command == 'p':
elif command == "p":
flag = input("Enter 1 to turn on PID and 0 to turn off:\n")
Car.pid(int(flag))
elif command == 'e':
print(int(Car.encoder().strip())) # need to strip character of \r or \n
elif command == 'q':
elif command == "e":
print(int(Car.encoder().strip())) # need to strip character of \r or \n
elif command == "q":
if Car.CarConnected:
del Car
break


27 changes: 13 additions & 14 deletions ECEnRacer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# python3 ECEnRacer.py
'''
"""
This program is for ECEN-631 BYU Race
*************** RealSense Package ***************
From the Realsense camera:
Expand All @@ -18,7 +18,7 @@
EXTREMELY IMPORTANT: Read the user manual carefully before operate the car
**************************************
'''
"""

# import the necessary packages
from Arduino import Arduino
Expand All @@ -27,15 +27,15 @@
import imutils
import cv2

rs = RealSense("/dev/video2", RS_VGA) # RS_VGA, RS_720P, or RS_1080P
rs = RealSense("/dev/video2", RS_VGA) # RS_VGA, RS_720P, or RS_1080P
writer = None

# Use $ ls /dev/tty* to find the serial port connected to Arduino
Car = Arduino("/dev/ttyUSB0", 115200) # Linux
#Car = Arduino("/dev/tty.usbserial-2140", 115200) # Mac
Car = Arduino("/dev/ttyUSB0", 115200) # Linux
# Car = Arduino("/dev/tty.usbserial-2140", 115200) # Mac

Car.zero(1500) # Set car to go straight. Change this for your car.
Car.pid(1) # Use PID control
Car.zero(1500) # Set car to go straight. Change this for your car.
Car.pid(1) # Use PID control
# You can use kd and kp commands to change KP and KD values. Default values are good.
# loop over frames from Realsense
while True:
Expand All @@ -44,21 +44,20 @@
cv2.imshow("RGB", rgb)
cv2.imshow("Depth", depth)

'''
"""
Add your code to process rgb, depth, IMU data
'''
"""

'''
"""
Control the Car
'''
"""

'''
"""
IMPORTANT: Never go full speed. Use CarTest.py to selest the best speed for you.
Car can switch between positve and negative speed (go reverse) without any problem.
'''
"""
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
del rs
del Car

38 changes: 24 additions & 14 deletions RealSense.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Camera.py
'''
"""
******* Realsense camera as the sensor ***************
The Intel Realsense 435i camera provides
RGB Data
Depth Data
Gyroscope Data
Accelerometer Data
***********************************************
'''
"""
# import the necessary packages
import pyrealsense2 as rs
import cv2
Expand All @@ -17,22 +17,28 @@
RS_VGA = 0
RS_720P = 1
RS_1080P = 2


class RealSense:
def __init__(self, Device, Resolution):
# configure rgb, depth, gyro, accel streams
# configure rgb, depth, gyro, accel streams
if Resolution == RS_720P:
rgbSize = [1280, 720]
depthSize = [1280, 720]
elif Resolution == RS_1080P:
elif Resolution == RS_1080P:
rgbSize = [1920, 1080]
depthSize = [1280, 720] # depth camera only allows upto 720P
depthSize = [1280, 720] # depth camera only allows upto 720P
else:
rgbSize = [640, 480]
depthSize = [640, 480]
self.pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.color, rgbSize[0], rgbSize[1], rs.format.bgr8, 30)
config.enable_stream(rs.stream.depth, depthSize[0], depthSize[1], rs.format.z16, 30)
config.enable_stream(
rs.stream.color, rgbSize[0], rgbSize[1], rs.format.bgr8, 30
)
config.enable_stream(
rs.stream.depth, depthSize[0], depthSize[1], rs.format.z16, 30
)
config.enable_stream(rs.stream.accel, rs.format.motion_xyz32f, 250)
config.enable_stream(rs.stream.gyro, rs.format.motion_xyz32f, 200)
# Start streaming
Expand All @@ -51,7 +57,7 @@ def gyro_data(self, gyro):
def accel_data(self, accel):
return np.asarray([accel.x, accel.y, accel.z])

def getData (self):
def getData(self):
# start realsense pipeline
rsframes = self.pipeline.wait_for_frames()
color_frame = rsframes.get_color_frame()
Expand All @@ -68,9 +74,13 @@ def getData (self):
# Update color and depth frames:ss
depth_frame = rsframes.get_depth_frame()
# Convert to numpy array
depth = cv2.normalize(~np.asanyarray(depth_frame.get_data()), None, 255, 0, cv2.NORM_MINMAX, dtype=cv2.CV_8U)
#depth = np.asanyarray(self.colorizer.colorize(depth_frame).get_data())
return(time.time(), rgb, depth, accel, gyro)



depth = cv2.normalize(
~np.asanyarray(depth_frame.get_data()),
None,
255,
0,
cv2.NORM_MINMAX,
dtype=cv2.CV_8U,
)
# depth = np.asanyarray(self.colorizer.colorize(depth_frame).get_data())
return (time.time(), rgb, depth, accel, gyro)

0 comments on commit e8b1f1f

Please sign in to comment.