#import libraries from gpiozero import CamJamKitRobot #if you don't have this board switch this with Robot from contours import get_saturated_colors, setup_camera #import from the contours program #Setup the robot, and camera function #These are the instructions if you don't own the CamJam Edukit 3 #Let's say you use the pins 27 and 17 for left and 24 and 23 for right. Then alter it as such: #robot = Robot(left=(27, 17), right=(24,23)) devastator_robot = CamJamKitRobot() camera, capture_buffer = setup_camera() #Set things up for movement for raw in camera.capture_continuous(capture_buffer, format="bgr"): image = raw.array masked, contours, found_color = get_saturated_colors( image) #This is called from the contours script print( f"Color {found_color}, h value: {found_color[0]}" ) #This will print the color name as well as the value in an RBG array if 5 < found_color[ 0] < 40: #This chooses the first value of the array. This chooses values based on the color wheel print("yellow") devastator_robot.backward() elif 100 < found_color[0] < 135: print("blue") devastator_robot.forward() else: devastator_robot.stop() capture_buffer.truncate(0)
# CamJam EduKit 3 - Robotics # Worksheet 3 - Motor Test Code import time # Import the Time library from gpiozero import CamJamKitRobot # Import the CamJam GPIO Zero Library robot = CamJamKitRobot() # Turn the motors on print('Starting') robot.forward() # Wait for 1 seconds time.sleep(1) # Turn the motors off robot.stop() print('Stopping')
# Then text will be added to the top of the rectangle using Hershey Simplex Font. You can use any other font you want #y will equal startY - 15 only if startY - 15 is less than 15, otherwise it will equal startY + 15 label = "{}: {:.2f}%".format(CLASSES[idx], confidence * 100) cv2.rectangle(frame, (startX, startY), (endX, endY), COLORS[idx], 2) y = startY - 15 if startY - 15 > 15 else startY + 15 cv2.putText(frame, label, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2) #If the robotOn variable is true and the classes chosen is a person move the robot forward if (CLASSES[15]) and not robotOn: devastator_robot.forward() robotOn = True #If the class does not equal person and the variable is false the robot will stop else: devastator_robot.stop() robotOn = False # show the output frame cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF # if the `q` key was pressed, break from the loop # This can use any key you want such as s or t or x if key == ord("q"): break # update the FPS counter fps.update() # stop the timer and display FPS information
# CamJam EduKit 3 - Robotics # Worksheet 9 – Obstacle Avoidance GPIO Zero version # by Neil Bizzell (@PiVangelist) from gpiozero import DistanceSensor, CamJamKitRobot # Import DistanceSensor and CamJamKitRobot Classes from the GPIOzero library from time import sleep # Import sleep function from the time library # define the pins for the motor (fixed in the GPIO Zero library) robot = CamJamKitRobot() # Define GPIO pins to use for the ultrasonic sensor sensor = DistanceSensor(echo=18, trigger=17) #define how to avoid the obstacle (simple version, just turn away) def AvoidObstacle(): robot.left() #turn left sleep(0.5) #repeat the next indented block forever while True: robot.forward()#drive forward sleep(0.1) if sensor.distance < 0.3: #check if sensor distance is less than 0.3m robot.stop() #stop the robot AvoidObstacle() #call the AvoidObstacle function
class Rover: def __init__(self): # defining pin for the sensor's parts. self.pinTrigger = 17 self.pinEcho = 18 self.pinLinefollower = 25 # distance conunter self.total_distance = 0 # distance measurment, starting at self.distance_away = 1 # motor speed self.motor_speed = .3 self.motorforward = (self.motor_speed, self.motor_speed) self.motorbackward = (-self.motor_speed, -self.motor_speed) self.motorleft = (self.motor_speed, 0) self.motorright = (0, self.motor_speed) self.sensor = DistanceSensor(echo = self.pinEcho, trigger = self.pinTrigger) self.rover = CamJamKitRobot() def get_distance(self): self.distance_away = self.sensor.distance * 100 print(self.distance_away) def move_forward(self): self.rover.value = self.motorforward time.sleep(.5) self.rover.stop() print("Moving Forward") def move_back(self): self.rover.value = self.motorbackward time.sleep(1) self.rover.stop() print("Moving Back") def move_left(self): self.rover.value = self.motorright # the diffrence between the bots time to move left and right are due to issues of the rovers wheels not touch the ground evenly time.sleep(3) self.rover.stop() print("Moving to the left") def move_right(self): self.rover.value = self.motorleft time.sleep(.5) self.rover.stop() print("Moving to the Right") def navigate(self): while(self.total_distance <= 10): print(self.total_distance) self.get_distance() # checking to see if there is an object infront of the rover. if(self.distance_away < 20): print(self.distance_away) self.get_distance() print("getting close to an obstacle") self.move_left() self.move_forward() self.move_right() else: self.move_forward() time.sleep(1) self.total_distance += 1 time.sleep(1)
#import libraries from gpiozero import CamJamKitRobot #import Robot only if you use a different board from contours import get_saturated_colors, setup_camera #import from the contours script #setup the robot as well as the camera function #If you don't use the CamJam Edukit 3 here are the instructions #Let's say you use the pins 27 and 17 for left and 24 and 23 for right. Then alter it as such: #robot = Robot(left=(27, 17), right=(24,23)) devastator_robot = CamJamKitRobot() camera, capture_buffer = setup_camera() #Like before this sets the robot up for raw in camera.capture_continuous(capture_buffer, format="bgr"): image = raw.array masked, contours, found_color = get_saturated_colors(image) #This is exactly invoked as in the last script. print(f"Color {found_color}, h value: {found_color[0]}") #Just like before it prints the color name and the value if 5 < found_color[0] < 40: print("yellow") devastator_robot.backward() elif 100 < found_color[0] < 135: print("blue") devastator_robot.forward() elif 165 < found_color[0] < 180: #Red is added here for expandability. Again, based on the color wheel and it chooses the first value of the array print("red") devastator_robot.right() else: devastator_robot.stop() #It stops if it sees no other color. capture_buffer.truncate(0)
myBot.stop() sleep(1) print('Obstacle Detected at ', sensor.distance * 100, 'cm. Change Course') sleep(1) myBot.backward() sensor.wait_for_out_of_range() myBot.stop() sleep(1) myBot.left(0.5) sleep(0.5) myBot.stop() sleep(1) try: while True: if sensor.distance > 0.15: driveForward() sensor.wait_for_in_range() avoidObstacle() else: print('Halt Program, objects are too close!') myBot.stop() break except KeyboardInterrupt: print('\n') print('Stopping Program') myBot.stop() myBot.close()