Пример #1
0
def rush_ball():
    global time_eclipse, rush
    car.go()
    time.sleep(0.8)
    time_eclipse = 0
    rush = False
    car.brake()
    car.remove_infrared_sensor_change(infrare_handler)
Пример #2
0
def move_around_ball_counterclockwise():
    global direction
    direction = False
    car.rotate_right_90()
    car.on_infrared_sensor_change(brake_if_touch_something)
    car.go()
    time.sleep(0.2)
    car.brake()
Пример #3
0
def push_ball():
    def delay_brake_after_touch_ball(status):
        if status[1]:
            scheduler.schedule('x', (20, car.brake))
            car.remove_infrared_sensor_change(delay_brake_after_touch_ball)

    car.on_infrared_sensor_change(delay_brake_after_touch_ball)
    car.go()
Пример #4
0
def go_ball(bound):
    global rush
    radius = max(bound[2], bound[3]) / 2
    center_y = bound[1] + bound[3] / 2
    bottom = center_y - radius
    car.set_left_wheels_speed(10)
    car.set_right_wheels_speed(10)
    if not car.registered_infrared_sensor_callback(infrare_handler):
        car.on_infrared_sensor_change(infrare_handler)
    if bottom <= 0:
        rush = True
        rush_ball()
    else:
        rush = True
        car.go()
def track_detector_callback(status):
    global avoid_flag
    # plan A
    left, middle, right = status
    global last_rotate
    if track_flag == 1:
        if left == 1:
            last_rotate = 1
            # car.set_global_speed(1)
            car.rotate_left()
        elif right == 1:
            last_rotate = 2
            # car.set_global_speed(1)
            car.rotate_right()
        elif middle == 1:
            if last_rotate == 1:
                scheduler.schedule('buchang', (0, car.rotate_right),
                                   (5, car.go))
            if last_rotate == 2:
                scheduler.schedule('buchang', (0, car.rotate_left),
                                   (5, car.go))

            last_rotate = 0
            car.set_global_speed(10)
            car.go()

    elif track_flag == 3:
        pass
    else:
        if middle == 1:
            scheduler.cancel('avoid_ob')
            avoid_flag = 0
            change_flag_normal()
            recover_wheel_to_normal()
            car.rotate_left()
            print('refind road')
def load():
    global loaded, avoid_flag, track_flag, last_rotate
    # car.set_global_speed(5)
    avoid_flag = 0
    track_flag = 1
    last_rotate = 0
    if not loaded:
        car.on_track_detector_change(track_detector_callback)
        # car.on_infrared_sensor_change(simple_avoid_ob)
        car.on_ultrasonic_in_range(simple_avoid_ob_from_ultrasonic, 0, 25)
        scheduler.start()
    loaded = True


def unload():
    global loaded
    if loaded:
        car.remove_track_detector_callback(track_detector_callback)
        # car.remove_infrared_sensor_change(simple_avoid_ob)
        car.remove_ultrasonic_callback(simple_avoid_ob_from_ultrasonic)
        scheduler.stop()
    loaded = False


if __name__ == '__main__':
    load()
    car.go()
    scheduler.start()
    # scheduler.schedule('stop',(2000,car.brake))
Пример #7
0
 def on_message(self, message):
     global car_mode
     if message == 'ping':
         # Reply with status of sensors
         infrared = car._last_infrared_sensor_status
         tracks = car._last_track_detector_status
         reply = []
         reply.append({
             'type': 'sensor',
             'data': {
                 'Left Infrared Sensor': infrared[0],
                 'Middle Infrared Sensor': infrared[1],
                 'Right Infrared Sensor': infrared[2],
                 'Left Track Detector': tracks[0],
                 'Middle Track Detector': tracks[1],
                 'Right Track Detector': tracks[2]
             }
         })
         while not ws_tasks.empty():
             reply.append(ws_tasks.get())
         self.write_message(json.dumps(reply))
     else:
         print("WebSocket on_message(not ping)")
         print(message)
         message = json.loads(message)
         if message['mode'] == 'track':
             if car_mode == 'ball':
                 print('Stop find ball')
                 if camera is not None:
                     camera.origin()
             print('Start find track')
             car_mode = 'track'
             car.set_global_speed(10)
             find_track_and_avoid.load()
             car.go()
         elif message['mode'] == 'ball':
             if car_mode == 'track':
                 print('Stop find track')
                 find_track_and_avoid.unload()
             print('Start find ball')
             car_mode = 'ball'
             car.set_global_speed(10)
             if camera is not None:
                 camera.mark()
             ball_utils.load()
         elif message['mode'] == 'user':
             if car_mode == 'track':
                 print('Stop find track')
                 find_track_and_avoid.unload()
             elif car_mode == 'ball':
                 print('Stop find ball')
                 if camera is not None:
                     camera.origin()
                 ball_utils.unload()
             car_mode = 'user'
             if message['speed'] == 0:
                 car.brake()
             else:
                 car.set_global_speed(message['speed'])
                 if message['direction'] == 0:
                     car.go()
                 elif message['direction'] == 180:
                     car.back()
                 elif message['direction'] == 270:
                     car.rotate_left()
                 elif message['direction'] == 90:
                     car.rotate_right()
         else:
             print('Invalid mode "{}", ignored.'.format(message['mode']))