def setMotorJoystickPos( self, joystickX, joystickY ): joystickX, joystickY = self.normaliseJoystickData( joystickX, joystickY ) if debug: print "Left joy",joystickX, joystickY print self.speed_l*joystickY gopigo.set_left_speed(int(self.speed_l*joystickY)) gopigo.fwd()
def turn_slight_right(): if msg_en: print("Turn slight right") if gpg_en: gopigo.set_right_speed(fwd_speed) gopigo.set_left_speed(slight_turn_speed) gopigo.fwd()
def __init__( self ): gopigo.set_speed(0) gopigo.fwd() self.lastServoSettingsSendTime = 0.0 self.lastUpdateTime = 0.0 self.lastMotionCommandTime = time.time()
def main(): hedge = MarvelmindHedge(tty="/dev/ttyACM0", adr=10, debug=False) # create MarvelmindHedge thread hedge.start() # start thread sleep(.1) start_pos = convert2D(hedge.position()) print hedge.position() print start_pos new_pos = start_pos dst = 0.0 gopigo.set_speed(100) gopigo.fwd() try: while (abs(dst) < 1): sleep(.1) new_pos = convert2D(hedge.position()) hedge.print_position() dst = distance(start_pos, new_pos) print "start: ", start_pos print "current: ", new_pos print "distance: ", dst except KeyboardInterrupt: hedge.stop() # stop and close serial port gopigo.stop() sys.exit() hedge.stop() gopigo.stop()
def turn_slight_left(): if msg_en: print "Turn slight left" if gpg_en: gopigo.set_right_speed(slight_turn_speed) gopigo.set_left_speed(fwd_speed) gopigo.fwd()
def turn_fast_right(): if msg_en: print("Turn fast left") if gpg_en: gopigo.set_right_speed(fast_turn_speed) gopigo.set_left_speed(fwd_speed) gopigo.fwd()
def rescue(): gopigo.stop() print("Rescue") time.sleep(1) gopigo.set_left_speed(130) gopigo.set_right_speed(130) gopigo.fwd() time.sleep(0.2)
def turn_left(): if msg_en: print("Turn left") if gpg_en: gopigo.enc_tgt(0, 1, 6) gopigo.fwd() time.sleep(1.1) curr = absolute_line_pos()
def setNeckJoystickPos( self, joystickX, joystickY ): #print "g" joystickX, joystickY = self.normaliseJoystickData( joystickX, joystickY ) if debug: print "Right joy",joystickX, joystickY print self.speed_r*joystickY gopigo.set_right_speed(int(self.speed_r*joystickY)) gopigo.fwd() self.lastMotionCommandTime = time.time()
def stop_until_safe_distance(): gopigo.stop() dist = get_dist() while (isinstance(dist, str) and dist != NOTHING_FOUND) or dist < SAFE_DISTANCE: dist = get_dist() gopigo.set_speed(0) gopigo.fwd()
def go_forward(forward_distance): gopigo.enable_encoders() encoder_steps_required = int(forward_distance / DISTANCE_PER_ENCODER_STEP) gopigo.set_speed(DEFAULT_ROBOT_SPEED) gopigo.enc_tgt(1, 1, encoder_steps_required) gopigo.fwd() wait_till_encoder_target_reached() RobotPosePrediction.currentRobotPose = RobotPosePrediction.getPredictedRobotPose(currentRobotPose = RobotPosePrediction.currentRobotPose, movementType = RobotPosePrediction.MOVEMENT_TYPE_FORWARD, movementAmount = encoder_steps_required * DISTANCE_PER_ENCODER_STEP) RobotPosePrediction.currentRobotLocationUncertainty = RobotPosePrediction.getPredictedRobotUncertainty(currentRobotLocationUncertainty = RobotPosePrediction.currentRobotLocationUncertainty, movementType = RobotPosePrediction.MOVEMENT_TYPE_FORWARD, movementAmount = encoder_steps_required * DISTANCE_PER_ENCODER_STEP, currentRobotPose = RobotPosePrediction.currentRobotPose)
def fwd_cm(dist=None): ''' Move chassis fwd by a specified number of cm. This function sets the encoder to the correct number of pulses and then invokes fwd(). ''' if dist is not None: pulse = int(cm2pulse(dist)) enc_tgt(1,1,pulse) fwd()
def get(self): left = int(self.get_argument("left")) right = int(self.get_argument("right")) gopigo.set_left_speed(abs(left)) gopigo.set_right_speed(abs(right)) if (left > 0): gopigo.fwd() else: gopigo.bwd()
def forward(step): if int(step) <= 20: print("Forward: " + str(step)) gopigo.fwd() time.sleep(int(step) * 0.5) # sleep for step * 0.5 second. gopigo.stop() return 'Bot moved forward!' else: error_message = "Invalid forward command or value is > 20" print(error_message) return error_message
def main(self, motor1, motor2): """ Takes in 2 ints and rotates accordingly """ gopigo.set_left_speed(motor1) gopigo.set_right_speed(motor2) if motor1 >= 0 and motor2 >= 0: gopigo.fwd() else: gopigo.bwd()
def move(self, dist, how="F"): """this takes care of moving the robot. how can be F: which is forward, dist is in cm L: which is left (right motor on left motor off), dist is in degrees R: which is right (left motor on right motor off), dist is in degrees B: which is backward. dist is in cm TL: rotate in place, to the left (tank steering) dist is in pulses TR: rotate in place, to the right (tank steering) dist is in pulses """ if (dist < 30 and (how == "F" or how == "B")): #If dist <30 the robot will #try to move 0 distance which means go for ever. Bad news! return -1 elif (dist < 8 and (how == "L" or how == "R")): #also the robot rotates for ever if the angle is less than 8 return -1 else: #take an initial record #self.recordEncoders() #save the current position because we're about to reset it #self.keyframe = self.timerecord[-1][:-1] #basically we are recording our position while moving. #prevtime = int(time.time()) #go forward! this runs in the background pretty much if (how == "F"): go.fwd(dist) elif (how == "L"): go.turn_left(dist) elif (how == "R"): go.turn_right(dist) elif (how == "B"): go.bwd(dist) elif (how == "TR"): go.enc_tgt(0, 1, dist) go.right_rot() elif (how == "TL"): go.enc_tgt(0, 1, dist) go.left_rot() #record while we haven't reached our destination #while(go.read_enc_status()): #this resets both encoders so we start counting from zero! #this next part should only trigger once per recordFreq #if(time.time()-prevtime>self.recordFreq): #prevtime = time.time() #make sure to reset the timer... #tell the recordEncoders function which direction #we are going #dir = "forward" #if(how=="B"): # dir = "backward" #self.recordEncoders(dir=dir) return 1
def __stop_until_safe_distance(self): """ Stops the rover until it achieves a safe distance from the obstacle. This can occur when/if the obstacle moves away from the rover. """ gopigo.stop() self.obstacle_distance = get_dist() while (isinstance(self.obstacle_distance, str) and self.obstacle_distance != NOTHING_FOUND) or \ self.obstacle_distance < self.safe_distance: self.obstacle_distance = get_dist() gopigo.set_speed(0) gopigo.fwd()
def turn_right(): if msg_en: print("Turn right") if gpg_en: gopigo.enc_tgt(0, 1, 15) gopigo.fwd() time.sleep(.9) gopigo.stop() time.sleep(1) gopigo.set_speed(80) gopigo.enc_tgt(1, 1, 35) gopigo.right_rot() time.sleep(.7) gopigo.stop() time.sleep(1) curr = absolute_line_pos() ic.right_turns += 1
def process_command(_command, _time): data = _command if not data: print "received data:", data if len(data) != 1: print ("Invalid command") return "Invalid command" elif data == 'w': gopigo.fwd() # return "Moving forward" elif data == 'x': gopigo.stop() # return "Stopping" elif data == 's': gopigo.bwd() # return "Moving back" elif data == 'a': gopigo.left() # return "Turning left" elif data == 'd': gopigo.right() # return "Turning right" elif data == 't': gopigo.increase_speed() # return "Increase speed" elif data == 'g': gopigo.decrease_speed() # return "Decrease speed" elif data == 'v': # print gopigo.volt(), 'V' return str(gopigo.volt()) elif data == 'l': gopigo.led_on(0) gopigo.led_on(1) time.sleep(1) gopigo.led_off(0) gopigo.led_off(1) return "Flash LED" else: print ("Invalid command") return "Invalid command" # run as a app if _time: time.sleep(_time) gopigo.stop()
def go_forward(forward_distance): gopigo.enable_encoders() encoder_steps_required = int(forward_distance / DISTANCE_PER_ENCODER_STEP) gopigo.set_speed(DEFAULT_ROBOT_SPEED) gopigo.enc_tgt(1, 1, encoder_steps_required) gopigo.fwd() wait_till_encoder_target_reached() RobotPosePrediction.currentRobotPose = RobotPosePrediction.getPredictedRobotPose( currentRobotPose=RobotPosePrediction.currentRobotPose, movementType=RobotPosePrediction.MOVEMENT_TYPE_FORWARD, movementAmount=encoder_steps_required * DISTANCE_PER_ENCODER_STEP) RobotPosePrediction.currentRobotLocationUncertainty = RobotPosePrediction.getPredictedRobotUncertainty( currentRobotLocationUncertainty=RobotPosePrediction. currentRobotLocationUncertainty, movementType=RobotPosePrediction.MOVEMENT_TYPE_FORWARD, movementAmount=encoder_steps_required * DISTANCE_PER_ENCODER_STEP, currentRobotPose=RobotPosePrediction.currentRobotPose)
def __main(self): """ Runs the main loop of the ACC. If an exception is thrown in the main loop or the ACC is killed via Ctrl+c then the rover is stopped in order to prevent a collision. """ try: gopigo.set_speed(0) gopigo.fwd() self.t = time.time() while self.power_on: self.__update_system_info() self.__process_commands() dt = time.time() - self.t self.t = time.time() self.__observe_obstacle(dt) self.__calculate_relevant_distances(dt) self.__validate_user_settings() self.__obstacle_based_acceleration_determination(dt) # Reset the speed if it becomes negative if self.speed < 0: self.speed = 0 # Reset the speed if it goes below the minimum speed if self.user_set_speed < MIN_SPEED: self.speed = 0 l_diff, r_diff = self.__straightness_correction() self.__actualize_power(l_diff, r_diff) # Catch any exceptions that occur except (KeyboardInterrupt, Exception): traceback.print_exc() gopigo.stop() # Stop to prevent a collision gopigo.stop()
def setMotorJoystickPos( self, joystickX, joystickY ): joystickX, joystickY = self.normaliseJoystickData( joystickX, joystickY ) if debug: print( "Left joy",joystickX, joystickY) if joystickX > 0.5: print( "Right") gopigo.left(50) elif joystickX <-0.5: print ("Left") gopigo.right50 elif joystickY > 0.5: print ("Fwd") gopigo.fwd(80) elif joystickY < -0.5: print ("Back") gopigo.bwd(80) else: print ("Stop") gopigo.fwd(0)
def setMotorJoystickPos( self, joystickX, joystickY ): joystickX, joystickY = self.normaliseJoystickData( joystickX, joystickY ) if debug: print "Left joy",joystickX, joystickY #print self.speed_l*joystickY #gopigo.set_left_speed(int(self.speed_l*joystickY)) #gopigo.fwd() if joystickX > .5: print "Left" gopigo.left() elif joystickX <-.5: print "Right" gopigo.right() elif joystickY > .5: print "Fwd" gopigo.fwd() elif joystickY < -.5: print "Back" gopigo.bwd() else: print "Stop" gopigo.stop()
def setMotorJoystickPos(self, joystickX, joystickY): joystickX, joystickY = self.normaliseJoystickData(joystickX, joystickY) if debug: print("Left joy", joystickX, joystickY) #print self.speed_l*joystickY #gopigo.set_left_speed(int(self.speed_l*joystickY)) #gopigo.fwd() if joystickX > .5: print("Left") gopigo.left() elif joystickX < -.5: print("Right") gopigo.right() elif joystickY > .5: print("Fwd") gopigo.fwd() elif joystickY < -.5: print("Back") gopigo.bwd() else: print("Stop") gopigo.stop()
def do_turn(d): global turn_angle, compass gopigo.set_left_speed(210) gopigo.set_right_speed(210) time.sleep(0.1) compass.turn_c(d) if d == "left": gopigo.turn_left_wait_for_completion(turn_angle) elif d == "around": around() gopigo.stop() else: try: gopigo.turn_right_wait_for_completion(turn_angle) except TypeError: do_turn(d) time.sleep(0.1) gopigo.fwd() drive_forwards(0.5)
def wait_for_button(): gopigo.stop() while (gopigo.digitalRead(button_pin) != 1): try: time.sleep(.5) except IOError: print ("Error") print "Button pressed!" gopigo.set_speed(gopigo_speed) distance = 100 while (distance > distance_from_body): try: distance = gopigo.us_dist(distance_sensor_pin) print ("Distance: " + str(distance)) time.sleep(.1) gopigo.fwd() except IOError: print ("Error") gopigo.stop() sound("Hello!")
def wait_for_button(): gopigo.stop() while (gopigo.digitalRead(button_pin) != 1): try: time.sleep(.5) except IOError: print("Error") print "Button pressed!" gopigo.set_speed(gopigo_speed) distance = 100 while (distance > distance_from_body): try: distance = gopigo.us_dist(distance_sensor_pin) print("Distance: " + str(distance)) time.sleep(.1) gopigo.fwd() except IOError: print("Error") gopigo.stop() sound("Hello!")
def go_straight(): if msg_en: print "Going straight" if gpg_en: gopigo.set_speed(fwd_speed) gopigo.fwd()
def fwd(aTicks): if checkTicks(aTicks): gpg.enc_tgt(1, 1, aTicks) gpg.fwd() waitForTarget()
def forward(): print("Forward!") gopigo.fwd() # Send the GoPiGo Forward time.sleep(1) # for 1 second. gopigo.stop() # the stop the GoPiGo return 'Alexabot moved forward!'
def process_command(self, command): parts = command.split("/") if parts[1] == "poll": print "poll" self.us_dist = gopigo.us_dist(usdist_pin) self.enc_status = gopigo.read_status()[0] self.volt = gopigo.volt() self.fw_ver = gopigo.fw_ver() self.trim = gopigo.trim_read() - 100 if self.enc_status == 0: self.waitingOn = None elif parts[1] == "stop": gopigo.stop() elif parts[1] == "trim_write": gopigo.trim_write(int(parts[2])) self.trim = gopigo.trim_read() elif parts[1] == "trim_read": self.trim = gopigo.trim_read() - 100 elif parts[1] == "set_speed": if parts[2] == "left": self.left_speed = int(parts[3]) elif parts[2] == "right": self.right_speed = int(parts[3]) else: self.right_speed = int(parts[3]) self.left_speed = int(parts[3]) gopigo.set_left_speed(self.left_speed) gopigo.set_right_speed(self.right_speed) elif parts[1] == "leds": val = 0 if parts[3] == "on": val = 1 elif parts[3] == "off": val = 0 elif parts[3] == "toggle": val = -1 if parts[2] == "right" or parts[2] == "both": if val >= 0: self.ledr = val else: self.ledr = 1 - self.ledr if parts[2] == "left" or parts[2] == "both": if val >= 0: self.ledl = val else: self.ledl = 1 - self.ledl gopigo.digitalWrite(ledr_pin, self.ledr) gopigo.digitalWrite(ledl_pin, self.ledl) elif parts[1] == "servo": gopigo.servo(int(parts[2])) elif parts[1] == "turn": self.waitingOn = parts[2] direction = parts[3] amount = int(parts[4]) encleft = 0 if direction == "left" else 1 encright = 1 if direction == "left" else 0 gopigo.enable_encoders() gopigo.enc_tgt(encleft, encright, int(amount / DPR)) if direction == "left": gopigo.left() else: gopigo.right() elif parts[1] == "move": self.waitingOn = int(parts[2]) direction = parts[3] amount = int(parts[4]) gopigo.enable_encoders() gopigo.enc_tgt(1, 1, amount) if direction == "backward": gopigo.bwd() else: gopigo.fwd() elif parts[1] == "beep": gopigo.analogWrite(buzzer_pin, self.beep_volume) time.sleep(self.beep_time) gopigo.analogWrite(buzzer_pin, 0) elif parts[1] == "reset_all": self.ledl = 0 self.ledr = 0 gopigo.digitalWrite(ledl_pin, self.ledl) gopigo.digitalWrite(ledr_pin, self.ledr) gopigo.analogWrite(buzzer_pin, 0) # gopigo.servo(90) gopigo.stop()
def fwd_button_OnButtonClick(self, event): f = gopigo.fwd()
def fwd_button_OnButtonClick(self,event): f=gopigo.fwd()
def forward(): print("Forward!") gopigo.fwd() # Send the GoPiGo Forward time.sleep(1) # for 1 second. gopigo.stop() # the stop the GoPiGo return render_template('forward.html')
def change_state(m_, t): global state, prev_marker, state_timer direction = None if m_ is not None: m = int(m_) else: m = None new_state = State.STOP if state == State.DRIVE: if m is None or m == -1: new_state = State.DRIVE elif m >= 0 and m != prev_marker: newPosition(m) new_state = State.STOP else: new_state = State.DRIVE elif state == State.STOP: prev_marker = m # Check if we waited for 1 second if time.time() - 1 > state_timer: # Get the direction from the algorithm direction = algoInstance.getDirection() # Save the current state, since we got new directions with open("last_state.pickle", "wb") as f_pickle: f_pickle.write(pickle.dumps(algoInstance)) if direction == algo.LEFT: new_state = State.TURN_LEFT elif direction == algo.RIGHT: new_state = State.TURN_RIGHT elif direction == algo.BACK: new_state = State.TURN_AROUND elif direction == algo.STOP: new_state = State.STOP elif direction == algo.STRAIGHT: new_state = State.DRIVE else: print("Broken direction:", direction) new_state = State.STOP else: new_state = State.STOP elif state == State.TURN_LEFT: if turn_done(): new_state = State.DRIVE else: new_state = State.TURN_LEFT elif state == State.TURN_RIGHT: if turn_done(): new_state = State.DRIVE else: new_state = State.TURN_RIGHT elif state == State.TURN_AROUND: if turn_done(): new_state = State.DRIVE else: new_state = State.TURN_AROUND # We changed state if new_state != state: if state == State.STOP and new_state == State.DRIVE: gopigo.fwd() state_timer = time.time() return new_state
def do_command(command=None): logging.debug(command) if command in ["forward", "fwd"]: gopigo.fwd() elif command == "left": gopigo.left() elif command == "left_rot": gopigo.left_rot() elif command == "right": gopigo.right() elif command == "right_rot": gopigo.right_rot() elif command == "stop": gopigo.stop() elif command == "leftled_on": gopigo.led_on(0) elif command == "leftled_off": gopigo.led_off(0) elif command == "rightled_on": gopigo.led_on(1) elif command == "rightled_off": gopigo.led_off(1) elif command in ["back", "bwd"]: gopigo.bwd() elif command == "speed": logging.debug("speed") speed = flask.request.args.get("speed") logging.debug("speed:" + str(speed)) if speed: logging.debug("in if speed") gopigo.set_speed(int(speed)) left_speed = flask.request.args.get("left_speed") logging.debug("left_speed:" + str(left_speed)) if left_speed: logging.debug("in if left_speed") gopigo.set_left_speed(int(left_speed)) right_speed = flask.request.args.get("right_speed") logging.debug("right_speed:" + str(right_speed)) if right_speed: logging.debug("in if right_speed") gopigo.set_right_speed(int(right_speed)) speed_result = gopigo.read_motor_speed() logging.debug(speed_result) return flask.json.jsonify({"speed": speed_result, "right": speed_result[0], "left": speed_result[1]}) elif command == "get_data": speed_result = gopigo.read_motor_speed() enc_right = gopigo.enc_read(0) enc_left = gopigo.enc_read(1) volt = gopigo.volt() timeout = gopigo.read_timeout_status() return flask.json.jsonify( { "speed": speed_result, "speed_right": speed_result[0], "speed_left": speed_result[1], "enc_right": enc_right, "enc_left": enc_left, "volt": volt, "timeout": timeout, "fw_ver": gopigo.fw_ver(), } ) elif command in ["enc_tgt", "step"]: tgt = flask.request.args.get("tgt") direction = flask.request.args.get("dir") if tgt: gopigo.gopigo.enc_tgt(1, 1, int(tgt)) if dir: if dir == "bwd": gopigo.bwd() else: gopigo.fwd() else: gopigo.fwd() return ""
import gopigo as go import time go.set_speed(130) go.fwd() go.led_on(0) go.led_on(1) time.sleep(1.9) go.led_off(0) go.led_off(1) go.right() go.led_on(0) time.sleep(0.2) go.fwd() go.led_on(0) go.led_on(1) time.sleep(0.8) go.led_off(0) go.led_off(1) go.left() go.led_on(1) time.sleep(0.8) for i in range(4): go.right() go.led_on(0) time.sleep(0.1) go.fwd() go.led_on(0) go.led_on(1)
def main(): global network, state, prev_marker, algoInstance if network is None: # Setup network ip = "localhost" with open("server.ip") as f: ip = f.read() x, y = 1, 0 network = netemuclient.NetEmuClient(rec, ip, 8080) network.start() network.waitForMaze() network.position(x, y) network.txpower(0.02) if algoInstance is None: # If the program is started with arguments, this will load the last saved state if len(sys.argv) > 1: with open("last_state.pickle", "rb") as f_pickle: algoInstance = pickle.loads(f_pickle.read()) algoInstance.restoreState(network) network.position(*algoInstance.position) # Start the algorithm without a saved state else: algoInstance = algo.Algorithm(network, (x, y)) # Give everything time to warm up time.sleep(2) gopigo.set_left_speed(0) gopigo.set_right_speed(0) gopigo.fwd() save_timer = time.time() # Save the latest encoder reading save_enc = (0, 0) while True: # Move in the alorithm algoInstance.step() # Call the latest camera results (marker, t) = aruco.get_result() # GoPiGo is not very stable, this block is just to make it stable if save_timer + 3 < time.time(): try: new_enc = (gopigo.enc_read(0), gopigo.enc_read(1)) except TypeError: print( "GoPiGo breaks when you enc read sometimes just restart the main, the state should be fine" ) gopigo.stop() time.sleep(0.2) gopigo.stop() main() # We have been stopping while we should be driving if new_enc == save_enc and state == State.DRIVE: rescue() save_enc = new_enc # Update the state state = change_state(marker, t) if state == State.DRIVE: drive_forwards(t) elif state == State.STOP: gopigo.stop() elif state == State.TURN_LEFT: do_turn("left") elif state == State.TURN_RIGHT: do_turn("right") elif state == State.TURN_AROUND: do_turn("around") time.sleep(0.001) else: raise ValueError
def go_straight(): if msg_en: print("Going straight") if gpg_en: gopigo.set_speed(fwd_speed) gopigo.fwd()
def fwd(dist): go.fwd(dist)
def drive(self): gopigo.fwd()
def fwd(kargs): r = {'return_value': gopigo.fwd()} return r
import gopigo import time # Assign pin 15 or A1 port to the IR sensor gopigo.ir_recv_pin(15) print "Press any button on the remote to control the GoPiGo" while True: ir_data_back=gopigo.ir_read_signal() if ir_data_back[0]==-1: #IO Error pass elif ir_data_back[0]==0: #Old signal pass else: sig=ir_data_back[1:] #Current signal from IR remote if sig[9]==82 and sig[10]==83: #Assign the button with 82 and 83 in position 9 and 10 in the signal to forward command print "fwd" gopigo.fwd() elif sig[9]==114 and sig[10]==115: print "left" gopigo.left() elif sig[9]==242 and sig[10]==243: print "right" gopigo.right() elif sig[9]==210 and sig[10]==211: print "back" gopigo.bwd() elif sig[9]==43 and sig[10]==42: print "Stop" gopigo.stop() time.sleep(.1)
s.listen(1) #Accept an incoming connection conn, addr = s.accept() print '\nConnection address:', addr while 1: #Check the data data = conn.recv(BUFFER_SIZE) if not data: break print "received data:", data if len(data) <> 1: print ("Invalid command") conn.send("Invalid command") elif data=='f': gopigo.fwd() conn.send("Moving forward") elif data=='s': gopigo.stop() conn.send("Stopping") elif data=='b': gopigo.bwd() conn.send("Moving back") elif data=='l': gopigo.left() conn.send("Turning left") elif data=='r': gopigo.right() conn.send("Turning right") else: print ("Invalid command")