Exemple #1
0
 def test_camera_tilt_config(self, cfg, expected):
     self.car = Car(self.pwm, self.GPIO, config={'camera': cfg})
     self.assertEqual(
         self.car.tilt_default,
         expected,
         f"Camera tilt {cfg}: {self.car.tilt_default} == {expected}",
         )
Exemple #2
0
 def test_steering_config(self, cfg, expected):
     self.car = Car(self.pwm, self.GPIO, config={'steering': cfg})
     self.assertEqual(
         self.car.rotation_default,
         expected,
         f"Steering {cfg}: {self.car.rotation_default} == {expected}",
         )
Exemple #3
0
 def test_motor_config(self, cfg, forward, backwards):
     self.car = Car(
         self.pwm,
         self.GPIO,
         config={'motor': {'flip_direction': cfg}},
         )
     self.assertEqual(
         self.car.forward,
         forward,
         f"Forward {cfg}: {self.car.forward} == {forward}",
         )
     self.assertEqual(
         self.car.backwards,
         backwards,
         f"Forward {cfg}: {self.car.backwards} == {backwards}",
         )
Exemple #4
0
    def test_init(self):
        self.car = Car(self.pwm, self.GPIO)
        self.assertEqual(self.pwm.log, [])
        self.assertEqual(self.GPIO.log, [])

        self.car.start()

        # Pan/Tilt gets intitialized; speed and steering do not
        pantilt_channels = self.car.pan_controller \
            + self.car.tilt_controller
        for channel in pantilt_channels:
            self.assertTrue(any(
                log[0] == 'write' and log[1][0] == channel
                for log in self.pwm.log
                ), f"Channel {channel} has not received it's initial command.")

        log = [
            line for line in self.GPIO.log
            if line[0] == 'setup']
        self.assertEqual(log, [
            ['setup', ([11, 13, 12, 15], 'OUT'), {}],
            ], "Wheel direction pins should be intialized.")
odometry = VisualOdometry(camera)
odometry.initFeatures(100, 150)
odometry.start()

config = {}
config_file = os.path.abspath(os.path.join(
    os.path.dirname(__file__), 'config.json'))
if os.path.exists(config_file):
    with open(config_file, 'r') as config_json:
        config = json.load(config_json)

GPIO.setmode(GPIO.BOARD)    # Number GPIOs by its physical location
pwm = PWM()                 # The servo controller.
pwm.frequency = 60

car = Car(pwm, GPIO, config=config)
car.start()

running = True
capture = False
last_snapshot = time.time()


class CarControl(SimpleHTTPRequestHandler):
    def shutdown(self):
        super(CarControl, self).shutdown()

    def do_GET(self):
        global running, capture, last_snapshot

        if self.path == '/camera':
    config = dict()
    args, ap = parse_args(config, False)
    if os.path.exists(args.config):
        # reparse args with defaults from config
        config = loadConfig(args.config)
    args, ap = parse_args(config)

    args.grid = tuple([int(g) for g in args.grid.split(",")])
    args.size = tuple([float(s) for s in args.size.split(",")])
    args.resolution = tuple([int(s) for s in args.resolution.split(",")])

    GPIO.setmode(GPIO.BOARD)  # Number GPIOs by its physical location
    pwm = PWM()  # The servo controller.
    pwm.frequency = 60

    car = Car(pwm, GPIO, config=config)
    car.start()

    camera = VideoCamera(
        src=args.index,
        patterns=args.patterns,
        resolution=args.resolution,
        framerate=args.framerate,
        usePiCamera=usePiCamera,
    )
    if args.npz is not None and os.path.exists(args.npz):
        camera.loadCalibration(args.npz)
    camera.start()

    calib = Calibration(args.pattern, args.grid, args.size)
Exemple #7
0
class TestCar(unittest.TestCase):

    def setUp(self):
        self.pwm = PWM()
        self.GPIO = GPIO()

    def tearDown(self):
        self.car.stop()

    @parameterized.expand([
        [[], [11, 13], [12, 15]],
        [[0], [12, 13], [11, 15]],
        [[1], [11, 15], [12, 13]],
        [[0, 1], [12, 15], [11, 13]],
        ])
    def test_motor_config(self, cfg, forward, backwards):
        self.car = Car(
            self.pwm,
            self.GPIO,
            config={'motor': {'flip_direction': cfg}},
            )
        self.assertEqual(
            self.car.forward,
            forward,
            f"Forward {cfg}: {self.car.forward} == {forward}",
            )
        self.assertEqual(
            self.car.backwards,
            backwards,
            f"Forward {cfg}: {self.car.backwards} == {backwards}",
            )

    @parameterized.expand([
        [{'rotation_offset': 100.0, 'bias': -10.0}, 90.0],
        [{'rotation_offset': 100.0}, 100.0],
        [{'rotation_offset': 100.0, 'bias': 0.0}, 100.0],
        [{'rotation_offset': 100.0, 'bias': 10.0}, 110.],
        ])
    def test_steering_config(self, cfg, expected):
        self.car = Car(self.pwm, self.GPIO, config={'steering': cfg})
        self.assertEqual(
            self.car.rotation_default,
            expected,
            f"Steering {cfg}: {self.car.rotation_default} == {expected}",
            )

    @parameterized.expand([
        [{'pan_default': 100.0, 'pan_bias': -10.0}, 90.0],
        [{'pan_default': 100.0}, 100.0],
        [{'pan_default': 100.0, 'pan_bias': 0.0}, 100.0],
        [{'pan_default': 100.0, 'pan_bias': 10.0}, 110.0],
        ])
    def test_camera_pan_config(self, cfg, expected):
        self.car = Car(self.pwm, self.GPIO, config={'camera': cfg})
        self.assertEqual(
            self.car.pan_default,
            expected,
            f"Camera pan {cfg}: {self.car.pan_default} == {expected}",
            )

    @parameterized.expand([
        [{'tilt_default': 100.0, 'tilt_bias': -10.0}, 90.0],
        [{'tilt_default': 100.0}, 100.0],
        [{'tilt_default': 100.0, 'tilt_bias': 0.0}, 100.0],
        [{'tilt_default': 100.0, 'tilt_bias': 10.0}, 110.0],
        ])
    def test_camera_tilt_config(self, cfg, expected):
        self.car = Car(self.pwm, self.GPIO, config={'camera': cfg})
        self.assertEqual(
            self.car.tilt_default,
            expected,
            f"Camera tilt {cfg}: {self.car.tilt_default} == {expected}",
            )

    def test_init(self):
        self.car = Car(self.pwm, self.GPIO)
        self.assertEqual(self.pwm.log, [])
        self.assertEqual(self.GPIO.log, [])

        self.car.start()

        # Pan/Tilt gets intitialized; speed and steering do not
        pantilt_channels = self.car.pan_controller \
            + self.car.tilt_controller
        for channel in pantilt_channels:
            self.assertTrue(any(
                log[0] == 'write' and log[1][0] == channel
                for log in self.pwm.log
                ), f"Channel {channel} has not received it's initial command.")

        log = [
            line for line in self.GPIO.log
            if line[0] == 'setup']
        self.assertEqual(log, [
            ['setup', ([11, 13, 12, 15], 'OUT'), {}],
            ], "Wheel direction pins should be intialized.")

    def test_speed(self):
        self.car = Car(self.pwm, self.GPIO)
        self.car.start()
        # Kill the thread; put the car in manual
        self.car.running = False

        self.GPIO.mock_reset_log()
        self.pwm.mock_reset_log()

        self.car.setSpeed(-1)
        self.car._update(self.car.getPosition(), 0.1)

        pwm_log = [
            line for line in self.pwm.log
            if line[1][0] in self.car.motor_speed_controller
            ]
        self.assertEqual(
            pwm_log,
            [
                ['write', (4, 0, 400), {}],
                ['write', (5, 0, 400), {}],
                ],
            f"Motor on {self.car.motor_speed_controller} are running now")

        gpio_log = [
            line for line in self.GPIO.log
            if line[0] in ['setup', 'output']]
        self.assertEqual(
            gpio_log,
            [['output', ([12, 15], 'HIGH'), {}]],
            "Backwards drive should be enabled")

        self.GPIO.mock_reset_log()
        self.pwm.mock_reset_log()

        self.car.setSpeed(.5)
        self.car._update(self.car.getPosition(), 0.1)

        pwm_log = [
            line for line in self.pwm.log
            if line[1][0] in self.car.motor_speed_controller
            ]
        self.assertEqual(
            pwm_log,
            [
                ['write', (4, 0, 0), {}],
                ['write', (5, 0, 0), {}],
                ],
            f"Motor on {self.car.motor_speed_controller} should stop first")

        gpio_log = [
            line for line in self.GPIO.log
            if line[0] in ['setup', 'output']]
        self.assertEqual(
            gpio_log,
            [['output', ([11, 13, 12, 15], 'LOW'), {}]],
            "Any direction drive should be disabled first")

        self.GPIO.mock_reset_log()
        self.pwm.mock_reset_log()
        self.car._update(self.car.getPosition(), 0.1)

        pwm_log = [
            line for line in self.pwm.log
            if line[1][0] in self.car.motor_speed_controller
            ]
        self.assertEqual(
            pwm_log,
            [
                ['write', (4, 0, 400), {}],
                ['write', (5, 0, 400), {}],
                ],
            f"Motor on {self.car.motor_speed_controller} are running again")

        gpio_log = [
            line for line in self.GPIO.log
            if line[0] in ['setup', 'output']]
        self.assertEqual(
            gpio_log,
            [['output', ([11, 13], 'HIGH'), {}]],
            "Forward drive should be enabled")

        self.GPIO.mock_reset_log()
        self.pwm.mock_reset_log()

        self.car.setSpeed(1)
        self.car._update(self.car.getPosition(), 0.1)

        pwm_log = [
            line for line in self.pwm.log
            if line[1][0] in self.car.motor_speed_controller
            ]
        self.assertEqual(
            pwm_log,
            [
                ['write', (4, 0, 800), {}],
                ['write', (5, 0, 800), {}],
                ],
            f"Motor on {self.car.motor_speed_controller} should go faster")

        gpio_log = [
            line for line in self.GPIO.log
            if line[0] in ['setup', 'output']]
        self.assertEqual(
            gpio_log,
            [],
            "Direction should not have changed")
Exemple #8
0
    def test_speed(self):
        self.car = Car(self.pwm, self.GPIO)
        self.car.start()
        # Kill the thread; put the car in manual
        self.car.running = False

        self.GPIO.mock_reset_log()
        self.pwm.mock_reset_log()

        self.car.setSpeed(-1)
        self.car._update(self.car.getPosition(), 0.1)

        pwm_log = [
            line for line in self.pwm.log
            if line[1][0] in self.car.motor_speed_controller
            ]
        self.assertEqual(
            pwm_log,
            [
                ['write', (4, 0, 400), {}],
                ['write', (5, 0, 400), {}],
                ],
            f"Motor on {self.car.motor_speed_controller} are running now")

        gpio_log = [
            line for line in self.GPIO.log
            if line[0] in ['setup', 'output']]
        self.assertEqual(
            gpio_log,
            [['output', ([12, 15], 'HIGH'), {}]],
            "Backwards drive should be enabled")

        self.GPIO.mock_reset_log()
        self.pwm.mock_reset_log()

        self.car.setSpeed(.5)
        self.car._update(self.car.getPosition(), 0.1)

        pwm_log = [
            line for line in self.pwm.log
            if line[1][0] in self.car.motor_speed_controller
            ]
        self.assertEqual(
            pwm_log,
            [
                ['write', (4, 0, 0), {}],
                ['write', (5, 0, 0), {}],
                ],
            f"Motor on {self.car.motor_speed_controller} should stop first")

        gpio_log = [
            line for line in self.GPIO.log
            if line[0] in ['setup', 'output']]
        self.assertEqual(
            gpio_log,
            [['output', ([11, 13, 12, 15], 'LOW'), {}]],
            "Any direction drive should be disabled first")

        self.GPIO.mock_reset_log()
        self.pwm.mock_reset_log()
        self.car._update(self.car.getPosition(), 0.1)

        pwm_log = [
            line for line in self.pwm.log
            if line[1][0] in self.car.motor_speed_controller
            ]
        self.assertEqual(
            pwm_log,
            [
                ['write', (4, 0, 400), {}],
                ['write', (5, 0, 400), {}],
                ],
            f"Motor on {self.car.motor_speed_controller} are running again")

        gpio_log = [
            line for line in self.GPIO.log
            if line[0] in ['setup', 'output']]
        self.assertEqual(
            gpio_log,
            [['output', ([11, 13], 'HIGH'), {}]],
            "Forward drive should be enabled")

        self.GPIO.mock_reset_log()
        self.pwm.mock_reset_log()

        self.car.setSpeed(1)
        self.car._update(self.car.getPosition(), 0.1)

        pwm_log = [
            line for line in self.pwm.log
            if line[1][0] in self.car.motor_speed_controller
            ]
        self.assertEqual(
            pwm_log,
            [
                ['write', (4, 0, 800), {}],
                ['write', (5, 0, 800), {}],
                ],
            f"Motor on {self.car.motor_speed_controller} should go faster")

        gpio_log = [
            line for line in self.GPIO.log
            if line[0] in ['setup', 'output']]
        self.assertEqual(
            gpio_log,
            [],
            "Direction should not have changed")