コード例 #1
0
def main():

    b = nxt.locator.find_one_brick()

    mc = MotCont(b)

    # mc.start()
    
    mc.cmd(3, 50, 720)

    light = Light(b, PORT_1)
    light.set_illuminated(True)


    # mc.stop()

    




    while True:
        try:
            print "Light sensor reading: ", light.get_sample()
            time.sleep(0.1)
        except KeyboardInterrupt:
            light.set_illuminated(False)
            sys.exit(1)
コード例 #2
0
ファイル: test_04.py プロジェクト: saschayoung/fessenden
class BasicMotion(object):

    def __init__(self):
        self.b = nxt.locator.find_one_brick()
        self.mc = MotCont(self.b)


    def wait_until_motors_ready(self):
        ready  = ( self.mc.is_ready(0) and self.mc.is_ready(1) )
        while not ready:
            # print "Not ready, motors still executing last command"
            ready  = ( self.mc.is_ready(0) and self.mc.is_ready(1) )





    def main(self):
        self.mc.start()
        time.sleep(0.2)

        # self.turn_in_place(-90)
        # time.sleep(0.2)
        # self.wait_until_motors_ready()


        self.move_forward(20.0)
        time.sleep(0.2)
        self.wait_until_motors_ready()

        self.turn_in_place(180)
        time.sleep(0.2)
        self.wait_until_motors_ready()

        self.move_forward(20.0)
        time.sleep(0.2)
        self.wait_until_motors_ready()

        self.turn_in_place(180)
        time.sleep(0.2)
        self.wait_until_motors_ready()

        self.mc.stop()
        


        # light = Light(self.b, PORT_1)
        # light.set_illuminated(True)
        # # self.turn_in_place(-90)
        # # motor_a = self.mc.is_ready(0)
        # # print "motor_a", motor_a
        # # time.sleep(2)
        # # motor_a = self.mc.is_ready(0)
        # # print "motor_a", motor_a
        # reflected_light = light.get_sample()

        # while True:
        #     reflected_light = light.get_sample()
        #     print "reflected light value: ", reflected_light

        #     if reflected_light > 500:
        #         self.mc.stop()
        #         print "lost line, stopping motion"
        #         break
        

        # self.stop_motion()
        # self.move_backward(10.0)    



        # for i in range(10):
        #     print "Light sensor reading: ", light.get_sample()
        #     time.sleep(0.1)

        # light.set_illuminated(False)


    #     except KeyboardInterrupt:
    #         sys.exit(1)

    def __servo_rotation(self, distance):
        wheel_diameter = 2.221
        return (180.0 * distance) / (math.pi * wheel_diameter / 2.0)
        
    def start_motion(self):
        self.mc.start()


    def stop_motion(self):
        self.mc.stop()
        # self.mc.set_output_state(0, 0, 0)
        # self.mc.set_output_state(1, 0, 0)


    def move_forward(self, distance, power = 50):
        wheel_rotation = self.__servo_rotation(distance)
        self.mc.cmd(3, power, int(wheel_rotation))
                

    def move_backward(self, distance, power = 50):
        wheel_rotation = self.__servo_rotation(distance)
        self.mc.cmd(3, -power, int(wheel_rotation))


    def turn_in_place(self, yaw, power = 50):
        axle_length = 4.5
        wheel_distance = (math.pi / 180.0) * abs(yaw) * (axle_length / 2.0)
        
        wheel_rotation = self.__servo_rotation(wheel_distance)

        if (yaw > 0):
            self.mc.cmd(0, power, int(wheel_rotation))
            self.mc.cmd(1, -power, int(wheel_rotation))
        elif (yaw < 0):
            self.mc.cmd(0, -power, int(wheel_rotation))
            self.mc.cmd(1, power, int(wheel_rotation))

        else:
            print "+++ melon melon melon +++"
コード例 #3
0
class BasicMotion(object):
    def __init__(self):
        self.b = nxt.locator.find_one_brick()
        self.mc = MotCont(self.b)

    def wait_until_motors_ready(self):
        ready = (self.mc.is_ready(0) and self.mc.is_ready(1))
        while not ready:
            # print "Not ready, motors still executing last command"
            ready = (self.mc.is_ready(0) and self.mc.is_ready(1))

    def main(self):
        self.mc.start()
        time.sleep(0.2)

        # self.turn_in_place(-90)
        # time.sleep(0.2)
        # self.wait_until_motors_ready()

        self.move_forward(20.0)
        time.sleep(0.2)
        self.wait_until_motors_ready()

        self.turn_in_place(180)
        time.sleep(0.2)
        self.wait_until_motors_ready()

        self.move_forward(20.0)
        time.sleep(0.2)
        self.wait_until_motors_ready()

        self.turn_in_place(180)
        time.sleep(0.2)
        self.wait_until_motors_ready()

        self.mc.stop()

        # light = Light(self.b, PORT_1)
        # light.set_illuminated(True)
        # # self.turn_in_place(-90)
        # # motor_a = self.mc.is_ready(0)
        # # print "motor_a", motor_a
        # # time.sleep(2)
        # # motor_a = self.mc.is_ready(0)
        # # print "motor_a", motor_a
        # reflected_light = light.get_sample()

        # while True:
        #     reflected_light = light.get_sample()
        #     print "reflected light value: ", reflected_light

        #     if reflected_light > 500:
        #         self.mc.stop()
        #         print "lost line, stopping motion"
        #         break

        # self.stop_motion()
        # self.move_backward(10.0)

        # for i in range(10):
        #     print "Light sensor reading: ", light.get_sample()
        #     time.sleep(0.1)

        # light.set_illuminated(False)

    #     except KeyboardInterrupt:
    #         sys.exit(1)

    def __servo_rotation(self, distance):
        wheel_diameter = 2.221
        return (180.0 * distance) / (math.pi * wheel_diameter / 2.0)

    def start_motion(self):
        self.mc.start()

    def stop_motion(self):
        self.mc.stop()
        # self.mc.set_output_state(0, 0, 0)
        # self.mc.set_output_state(1, 0, 0)

    def move_forward(self, distance, power=50):
        wheel_rotation = self.__servo_rotation(distance)
        self.mc.cmd(3, power, int(wheel_rotation))

    def move_backward(self, distance, power=50):
        wheel_rotation = self.__servo_rotation(distance)
        self.mc.cmd(3, -power, int(wheel_rotation))

    def turn_in_place(self, yaw, power=50):
        axle_length = 4.5
        wheel_distance = (math.pi / 180.0) * abs(yaw) * (axle_length / 2.0)

        wheel_rotation = self.__servo_rotation(wheel_distance)

        if (yaw > 0):
            self.mc.cmd(0, power, int(wheel_rotation))
            self.mc.cmd(1, -power, int(wheel_rotation))
        elif (yaw < 0):
            self.mc.cmd(0, -power, int(wheel_rotation))
            self.mc.cmd(1, power, int(wheel_rotation))

        else:
            print "+++ melon melon melon +++"
コード例 #4
0
class BasicMotion(object):
    def __init__(self):
        self.is_started = False
        self.is_stopped = True

        self.b = nxt.locator.find_one_brick()
        self.mc = MotCont(self.b)

    def wait_until_motors_ready(self):
        ready = (self.mc.is_ready(0) and self.mc.is_ready(1))
        while not ready:
            # print "Not ready, motors still executing last command"
            ready = (self.mc.is_ready(0) and self.mc.is_ready(1))

    def run(self):
        self.start_motion()

        self.init_light_sensor()

        while True:
            self.follow_line()
            self.find_line()

    def follow_line(self):
        print "is_started: ", self.is_started
        self.start_motion()
        print "is_started: ", self.is_started
        self.wait_until_motors_ready()

        print "following line"
        self.move_forward(150.0)
        while True:
            reflected_light = self.get_light_reading()
            if reflected_light > 500:
                self.stop_motion()
                print "lost line, stopping motion"
                return

    def find_line(self):
        print "is_started: ", self.is_started
        self.start_motion()
        print "is_started: ", self.is_started
        self.wait_until_motors_ready()

        print "finding line"
        sweep_angle = [-10, 20, -30, 40, -60, 80, -120, 160, -240, 320]
        line_found = False
        is_turning = True

        while not line_found:
            for sweep in sweep_angle:
                self.turn_in_place(sweep)
                is_turning = True

                while is_turning:
                    reflected_light = self.get_light_reading()
                    if reflected_light < 400:
                        line_found = True
                        self.stop_motion()
                        print "found line, stopping search"
                        return

                    motor_a_ready = self.mc.is_ready(0)
                    if motor_a_ready:
                        is_turning = False

    def init_light_sensor(self):
        self.light = Light(self.b, PORT_1)

    def get_light_reading(self, illumination=True):
        self.light.set_illuminated(illumination)
        return self.light.get_sample()

    def __servo_rotation(self, distance):
        wheel_diameter = 2.221
        return (180.0 * distance) / (math.pi * wheel_diameter / 2.0)

    def start_motion(self):
        if self.is_started == False:
            self.mc.start()
            self.is_started = True
            time.sleep(0.5)
        else:
            pass

    def stop_motion(self):
        self.mc.stop()
        self.is_started = False
        time.sleep(0.2)

    def move_forward(self, distance, power=50):
        wheel_rotation = self.__servo_rotation(distance)
        self.mc.cmd(3, power, int(wheel_rotation))

    def move_backward(self, distance, power=50):
        wheel_rotation = self.__servo_rotation(distance)
        self.mc.cmd(3, -power, int(wheel_rotation))

    def turn_in_place(self, yaw, power=50):
        axle_length = 4.5
        wheel_distance = (math.pi / 180.0) * abs(yaw) * (axle_length / 2.0)

        wheel_rotation = self.__servo_rotation(wheel_distance)

        if (yaw > 0):
            self.mc.cmd(0, power, int(wheel_rotation))
            self.mc.cmd(1, -power, int(wheel_rotation))
        elif (yaw < 0):
            self.mc.cmd(0, -power, int(wheel_rotation))
            self.mc.cmd(1, power, int(wheel_rotation))

        else:
            print "+++ melon melon melon +++"
コード例 #5
0
class Robot(Particle):
    def __init__(self, maze, bot):
        super(Robot, self).__init__(180, 35, 90)
        #super(Robot, self).__init__(*maze.random_free_place())
        self.step_count = 0

        # NXT specific
        self.motcont = MotCont(bot)
        self.motcont.start()
        self.sonar = nxt.Ultrasonic(bot, nxt.PORT_3)

        self.step = 360
        self.march_power = 80
        self.turn_power  = 50
        self.speed = 10

        #self.choose_random_direction()

    def choose_random_direction(self):
        heading = random.uniform(0, 360)
        self.turn_inplace(((heading - self.h) % 360) * 6)
        print("heading :" + repr(heading))
        self.h = heading

    def read_sensor(self, maze):
        # Sonar is not exactly at the robot's baricenter.
        s = self.sonar.get_sample() + ROBOT_LENGTH
        print("sonar: " + repr(s - ROBOT_LENGTH))
        return s

    def turn_inplace(self, degrees):
        self.motcont.cmd(nxt.motor.PORT_A,  self.turn_power, degrees, True, True)
        self.motcont.cmd(nxt.motor.PORT_C, -self.turn_power, degrees, True, True)
        while not (self.motcont.is_ready(nxt.motor.PORT_A) and self.motcont.is_ready(nxt.motor.PORT_C)):
            time.sleep(0.01)

    def move_ahead(self, tacholimit):
        self.motcont.cmd(nxt.motor.PORT_A, self.march_power, tacholimit, True, True)
        self.motcont.cmd(nxt.motor.PORT_C, self.march_power, tacholimit, True, True)
        while not (self.motcont.is_ready(nxt.motor.PORT_A) and self.motcont.is_ready(nxt.motor.PORT_C)):
            time.sleep(0.01)

    def advance_by(self, speed, checker=None):
        h = self.h
        r = math.radians(h)
        dx = math.cos(r) * self.speed
        dy = math.sin(r) * self.speed
        if checker is None or checker():
            self.move_ahead(self.step)
            self.move_by(dx, dy)
            return True
        return False

    def move(self, maze):
        while True:
            self.step_count += 1
            if self.advance_by(self.speed,
                checker=lambda: self.sonar.get_sample() > 20):
                break
            # Bumped into something or too long in same direction,
            # choose random new direction
            self.choose_random_direction()