def start(port): from pycreate2 import Create2 # Create a Create2. bot = Create2(port) bot.start() # Start the Create 2 #bot.start() # bot.wake() # bot.power() bot.full() # You are responsible for handling issues, no protection/safety in # this mode ... becareful # bot.full() # time.sleep(1) # directly set the motor speeds ... move forward # bot.drive_direct(100, 100) # time.sleep(1) # Stop the bot # bot.drive_stop() print("turning on pump...") bot.pump_on() time.sleep(1) print("turning pump off") bot.pump_off() print("closing") bot.close()
def rangestate(): port = '/dev/ttyUSB0' baud = {'default': 115200, 'alt': 19200} bot = Create2(port=port, baud=baud['default']) bot.start() bot.full() pub = rospy.Publisher('sensor_msg', Int32MultiArray, queue_size=10) rospy.init_node('rangestate', anonymous=True) rate = rospy.Rate(1) # 10hz while not rospy.is_shutdown(): sensor = bot.get_sensors() x = sensor[35] y = ( int(x[0]), int(x[1]), int(x[2]), int(x[3]), int(x[4]), int(x[5]), ) z = Int32MultiArray(data=y) #print(type(sensor.light_bumper)) #state = "Sensors: %s" %str(sensor.light_bumper) #rospy.loginfo(z) pub.publish(z) rate.sleep()
def sensor_state_publisher(): rospy.init_node('sensor_state') pub = rospy.Publisher('sensor_values', String, queue_size=10) rate = rospy.Rate(2) #read sensor range at every 2 seconds port = '/dev/ttyUSB0' baud = { 'default': 115200, 'alt': 19200 # shouldn't need this unless you accident$ } bot = Create2(port=port, baud=baud['default']) bot.start() bot.full() print('Starting ...') sensor_state = [0, 0, 0, 0, 0, 0] while not rospy.is_shutdown(): # Packet 100 contains all sensor data. sensor_state[0] = bot.get_sensors().light_bumper_left sensor_state[1] = bot.get_sensors().light_bumper_front_left sensor_state[2] = bot.get_sensors().light_bumper_center_left sensor_state[3] = bot.get_sensors().light_bumper_center_right sensor_state[4] = bot.get_sensors().light_bumper_front_right sensor_state[5] = bot.get_sensors().light_bumper_right sensor_string = str( ' %d %d %d %d %d %d ' % (sensor_state[0], sensor_state[1], sensor_state[2], sensor_state[3], sensor_state[4], sensor_state[5])) rospy.loginfo(sensor_string) pub.publish(sensor_string) rate.sleep()
def angle_state_publisher(): rospy.init_node('angle_state') pub = rospy.Publisher('angle_values', Int32, queue_size=10) rate = rospy.Rate(2) #read sensor range at every 2 seconds port = '/dev/ttyUSB0' baud = { 'default': 115200, 'alt': 19200 # shouldn't need this unless you accident$ } bot = Create2(port=port, baud=baud['default']) bot.start() bot.full() print('Starting ...') angle_state = 0 while not rospy.is_shutdown(): # Packet 100 contains all sensor data. angle_state = bot.get_sensors().angle rospy.loginfo(angle_state) pub.publish(angle_state) rate.sleep()
def __init__(self, port, key): geckopy.init_node() self.pub = geckopy.pubBinderTCP(key, 'create') self.sub = geckopy.subConnectTCP(key, 'cmd') # Create a Create2 self.bot = Create2(port) self.bot.start()
def main(): if True: sp = GeckoSimpleProcess() sp.start(func=core, name='geckocore', kwargs={}) print("<<< Starting Roomba >>>") port = "/dev/serial/by-id/usb-FTDI_FT231X_USB_UART_DA01NX3Z-if00-port0" bot = Create2(port) bot.start() bot.full() geckopy.init_node() rate = geckopy.Rate(10) # loop rate # s = geckopy.subBinderUDS(key, 'cmd', "/tmp/cmd") s = geckopy.subBinderTCP(key, 'cmd') if s is None: raise Exception("subscriber is None") # p = geckopy.pubBinderUDS(key,'create2',"/tmp/create") p = geckopy.pubBinderTCP(key, 'create2') if p is None: raise Exception("publisher is None") print("<<< Starting Loop >>>") try: bot.drive_direct(200, 200) while not geckopy.is_shutdown(): sensors = bot.get_sensors() # returns all data batt = 100 * sensors.battery_charge / sensors.battery_capacity # print(">> batter: {:.1f}".format(batt)) bot.digit_led_ascii("{:4}".format(int(batt))) # bot.digit_led_ascii("80") # print(">> ir:", sensors.light_bumper) # print(">> ir:", end=" ") # for i in range(6): # print("{:.1f}".format(sensors[35 + i]), end=" ") # print(" ") # msg = sensors # p.publish(msg) msg = s.recv_nb() if msg: print(msg) rate.sleep() except KeyboardInterrupt: print("bye ...") bot.drive_stop() # time.sleep(1) # bot.close() print("<<< Exiting >>>")
def callback(data): m=data.data #rospy.loginfo(m) print(m) port = '/dev/ttyUSB0' baud = { 'default': 115200, 'alt': 19200 # shouldn't need this unless you accident$ } bot = Create2(port=port, baud=baud['default']) bot.start() bot.full() while not rospy.is_shutdown(): bot.drive_direct(m,m)
def run_robot(ROBOT_DEVICE, odometry, command, state, STATES): # create Create2 object bot = Create2(port=ROBOT_DEVICE, baud=115200) # start robot bot.start() # robot drive in safe mode (cliff and wheel drop detection on) bot.safe() play_start_music(bot) print('ROBOT::start') while command[0] != b'qa': # if start robot, go find wall if state[0] is STATES[ 'find_wall']: # if in find wall mode, go find the wall find_wall(bot, odometry, command, state, STATES) # if wall reached, go follow wall elif state[0] is STATES['follow_wall']: follow_wall(bot, odometry, command, state, STATES) # o.w. follow the wall print('ROBOT::thread ends')
def interface2robot(orch_2_robot_control, robot_semaphore, interrupt): port = "/dev/ttyUSB0" bot = Create2(port) bot.start() bot.full() try: while True: robot_semaphore.acquire() local_command = deepcopy(orch_2_robot_control) orch_2_robot_control[4] = 0 robot_semaphore.release() start_time = time.time() goal_time = start_time+local_command[1] timecomplete = False if local_command[4] == 1: print("[DEBUG] interface2robot new command = {}" .format(local_command)) if local_command[2] != 0.0: bot.drive_direct(-100,100) time.sleep(1.6/90 * local_command[2]) bot.drive_direct(0,0) time.sleep(0.1) # bot.drive_distance(distance = local_command[3], speed=100, stop=True) bot.drive_direct(int(local_command[3]), int(local_command[3])) while (not interrupt[0] and not timecomplete): if(time.time() >= goal_time): timecomplete = True interrupt[0] = False bot.drive_direct(0,0) time.sleep(0.2) except KeyboardInterrupt: print("[INFO] interface2robot finished working") bot.drive_direct(0, 0)
def __init__(self): port = "/dev/ttyUSB0" self.speed = 200 self.bot = Create2(port) self.bot.start() self.bot.full()
from pynput import keyboard from pycreate2 import Create2 import time #Robot initialization________________________________________________________________ if __name__ == "__main__": port = '/dev/ttyUSB0' baud = { 'default': 115200, 'alt': 19200 # shouldn't need this unless you accidentally set it to this } bot = Create2(port=port, baud=baud['default']) bot.start() bot.safe() bot.full() print('Starting ...') bot.safe() cnt = 0 init_flag = 0 coll_flag = 0 lightmax = 0 #Key board controls__________________________________________________________ def on_press(key):
def drive(action, state_enemy): print("Chosen action: {0}".format(action)) bot = Create2('/dev/ttyUSB0', 115200) bot.start() bot.safe() if (action == "F"): # hier moet mijn gyro code komen die zorgt dat hij altijd even ver rijdt bot.drive_distance(1, 100) bot.drive_stop() bot.close() state_enemy += 11 done = check_victory(state_enemy) elif (action == "B"): # hier moet mijn gyro code komen die zorgt dat hij altijd even ver rijdt bot.drive_distance(-1, 100) bot.drive_stop() bot.close() state_enemy -= 11 done = check_victory(state_enemy) elif (action == "L"): # hier moet mijn gyro code komen die zorgt dat hij altijd even ver rijdt bot.turn_angle(90, 50) bot.drive_stop() bot.safe() bot.drive_distance(1, 100) bot.turn_angle(-90, 50) bot.drive_stop() bot.close() state_enemy -= 1 done = check_victory(state_enemy) elif (action == "R"): # hier moet mijn gyro code komen die zorgt dat hij altijd even ver rijdt bot.turn_angle(-90, 50) bot.drive_stop() bot.safe() bot.drive_distance(1, 100) bot.turn_angle(90, 50) bot.drive_stop() bot.close() state_enemy += 1 done = check_victory(state_enemy) elif (action == "S"): # hier moet mijn gyro code komen die zorgt dat hij altijd even ver rijdt state_enemy = state_enemy done = check_victory(state_enemy) else: pass return state_enemy, done
from pycreate2 import Create2 import time # Create a Create2. bot = Create2(port='/dev/serial0') # Start the Create 2 bot.start() # Put the Create2 into 'safe' mode so we can drive it # This will still provide some protection bot.safe() # You are responsible for handling issues, no protection/safety in # this mode ... becareful # bot.full() # directly set the motor speeds ... easier if using a joystick bot.drive_direct(100, 100) bot.drive_straight(50) time.sleep(5) # Stop the bot bot.drive_stop() # Close the connection bot.close()
from pycreate2 import Create2 import time # Create a Create2. port = "COM6" bot = Create2(port) # Start the Create 2 bot.start() # Put the Create2 into 'safe' mode so we can drive it # This will still provide some protection bot.safe() # You are responsible for handling issues, no protection/safety in # this mode ... becareful bot.full() # directly set the motor speeds ... move forward bot.drive_direct(100, 100) time.sleep(2) # turn in place bot.drive_direct(200, -200) # inputs for motors are +/- 500 max time.sleep(2) # Stop the bot bot.drive_stop() # query some sensors sensors = bot.get_sensors() # returns all data
def initBot(port): bot = Create2(port) bot.start() bot.full() return bot
from pycreate2 import Create2 from time import sleep bot = Create2('/dev/serial/by-id/usb-FTDI_FT231X_USB_UART_DN026EMT-if00-port0') bot.start() bot.full() sensors = bot.get_sensors() print(sensors) print(sensors.battery_charge, sensors.battery_capacity) # bot.drive_distance(.1, speed=40, stop=True) # bot.drive_turn(40, 1) # sleep(2) # # bot.drive_turn(40, -1) # sleep(2) bot.turn_angle(-3, speed=40) bot.stop()
#! /usr/bin/env python3 """ Example: Publish Roomba OI library via XMLRPC """ from xmlrpc.server import SimpleXMLRPCServer from pycreate2 import Create2 from settings import Settings # Instantiate iRobot OI cr2 = Create2(Settings.ROOMBA_SERIAL) cr2.open() # Create server with SimpleXMLRPCServer(('', Settings.XMLRPC_PORT), allow_none=True) as server: server.register_introspection_functions() server.register_multicall_functions() server.register_instance(cr2) server.serve_forever()
from pycreate2 import Create2 import sys, os, time # See README on how to find the port PORT = "/dev/ttyUSB0" # Don't remove the /dev/ portion # Create a Create2 bot = Create2(PORT) def main(): # Start the Create 2 bot.start() # The Create has several modes, Off, Passive, Safe, and Full. # Safe: Roomba stops when it detects a cliff, detects a wheel drop, or if on the charger # Full: Roomba does not stop when it encounters an event above # Passive: Roomba sends sensor data but does not accept changes to sensors or wheels # https://cdn-shop.adafruit.com/datasheets/create_2_Open_Interface_Spec.pdf bot.safe() # directly set the motor speeds ... easier if using a joystick bot.drive_direct(5, 5) # turn an angle [degrees] at a speed: 45 deg, 100 mm/sec bot.turn_angle(45, 100)
def drive(action, state_friendly): action = actions[action] bot = Create2('/dev/ttyUSB0', 115200) bot.start() bot.safe() if (action == "F"): # hier moet mijn gyro code komen die zorgt dat hij altijd even ver rijdt bot.drive_distance(1, 100) # kan zijn dat dit niet gaat werken en ik dit naar nex_state moet hernoemen, eens kijken bot.drive_stop() bot.close() state_friendly += 11 done = check_victory(state_friendly) elif (action == "B"): # hier moet mijn gyro code komen die zorgt dat hij altijd even ver rijdt bot.drive_distance(-1, 100) bot.drive_stop() bot.safe() bot.close() state_friendly -= 11 done = check_victory(state_friendly) elif (action == "L"): # hier moet mijn gyro code komen die zorgt dat hij altijd even ver rijdt bot.turn_angle(90, 50) bot.drive_stop() bot.safe() bot.drive_distance(1, 100) bot.turn_angle(-90, 50) bot.drive_stop() bot.close() state_friendly -= 1 done = check_victory(state_friendly) elif (action == "R"): # hier moet mijn gyro code komen die zorgt dat hij altijd even ver rijdt bot.turn_angle(-90, 50) bot.drive_stop() bot.safe() bot.drive_distance(1, 100) bot.turn_angle(90, 50) bot.drive_stop() bot.close() state_friendly += 1 done = check_victory(state_friendly) elif (action == "S"): # hier moet mijn gyro code komen die zorgt dat hij altijd even ver rijdt state_friendly = state_friendly bot.close() done = check_victory(state_enemy) else: pass return state_friendly, done
def random_walk(bot, ser): while True: if (pause): time.sleep(1) continue sensors = bot.get_sensors() bp = sensors.light_bumper bp_list = [ bp.left, bp.front_left, bp.center_left, bp.center_right, bp.front_right, bp.right ] print(bp_list) num_falses = bp_list.count(False) if num_falses == 6: free_walk(bot, ser) else: turn_90(bot, bp_list, ser) x_pos, y_pos = 62, 62 if __name__ == '__main__': ser = serial.Serial(port='/dev/ttyUSB0', baudrate=115200) bot = Create2('/dev/ttyUSB0', 115200) bot.start() bot.safe() # x_pos, y_pos = 62, 62 # cur_ang = 0 # 0-359 random_walk(bot, ser)
#!/usr/bin/env python # ---------------------------------------------------------------------------- # MIT License # shows how to get sensor data from the create 2 from __future__ import print_function from pycreate2 import Create2 import time if __name__ == "__main__": config = {} config["transport"] = 'echo' config["robot"] = 'Create2' bot = Create2(config) bot.start() bot.safe() print('Starting ...') while True: # Packet 100 contains all sensor data. sensor_state = '' try: sensor_state = bot.get_sensors() except: pass print('==============Updated Sensors==============') print(sensor_state)
import cv2 from pycreate2 import Create2 import time from ground_line import process_frame, WebcamVideoStream bot = Create2("/dev/ttyUSB0") bot.start() bot.full() bot.drive_direct(0.2, 0.2) time.sleep(5) """cap = cv2.VideoCapture(1) cap.set(3, 600) cap.set(4, 400)""" cap = WebcamVideoStream(src=1, size=(600, 400)) cap.start() while True: img = cap.read()[1] horiz_offset, angle_offset, frame = process_frame(img) s = bot.get_sensors().bumps_wheeldrops if s.bump_left or s.bump_right: bot.drive_direct(0.0, 0.0) break if horiz_offset is None: bot.drive_direct(0.0, 0.0) continue
def __init__(self, port='/dev/ttyUSB0', baud=115200): self.bot = Create2(port, baud=baud) self.bot.start() self.bot.full() self.bot.drive_stop() self._bumped = False