Beispiel #1
0
from spike import PrimeHub, LightMatrix, Button, StatusLight, ForceSensor, MotionSensor, Speaker, ColorSensor, App, DistanceSensor, Motor, MotorPair
from spike.control import wait_for_seconds, wait_until, Timer
from math import *

hub = PrimeHub()

motor_pair = MotorPair('A', 'B')  # Set the motor ports in the motor_pair.

motor_pair.set_default_speed(30)  # Set the default speed of the motor_pair.

# Set the distance that the robot travels for one rotation its wheels. The value comes from# the diameter of the wheel  multiplied by "π" (3.14).
motor_pair.set_motor_rotation(22, 'cm')

# Activate the brakes when the robot stops. The other conditions are 'hold' and 'coast'.
motor_pair.set_stop_action('brake')

wait_for_seconds(1)  # Wait for one second.

#Reset the Gyro sensor. The current yaw angle value is equal to 0.
hub.motion_sensor.reset_yaw_angle()

motor_pair.start(
    steering=100
)  # Turn left around the center of the wheelbase. Leftward because of steering=-100 parameter.


# To program the robot to wait until the robot has turned, we need to define a fuction that checks if the robot has turned.
def left_turn_end():  # Define the function
    return hub.motion_sensor.get_yaw_angle(
    ) > 90  # Return true or false depending on the yaw angle value.
Beispiel #2
0
from spike import PrimeHub, LightMatrix, Button, StatusLight, ForceSensor, MotionSensor, Speaker, ColorSensor, App, \
    DistanceSensor, Motor, MotorPair
from spike.control import wait_for_seconds, wait_until, Timer
import math

g_hub = PrimeHub()
g_right_sensor = ColorSensor('D')
g_motor_pair = MotorPair('A', 'E')
g_wheel_distance_apart = 14.5
g_wheel_radius = 4.25
g_wheel_circumference = 2 * math.pi * g_wheel_radius
g_motor_pair.set_motor_rotation(g_wheel_circumference, 'cm')
g_motor_pair.set_default_speed(-50)
g_motor_pair.set_stop_action('break')


def line_follow(hub, motor_pair, right_sensor, num):
    count = 0
    while count < num:
        if right_sensor.get_reflected_light() > 50:
            motor_pair.start_tank(-25, 15)
        else:
            motor_pair.start_tank(20, 15)
        count += 1


line_follow(g_hub, g_motor_pair, g_right_sensor, 200)
Beispiel #3
0
def closeGrip():
    motor_e.set_default_speed(50)
    motor_e.run_for_rotations(+1.50)


def openGrip():
    motor_e.set_default_speed(50)
    motor_e.run_for_rotations(-1.50)


# set up hub, motors and sensors
hub = PrimeHub()
motor_pair = MotorPair('A', 'B')
motor_pair.set_motor_rotation(17.5, 'cm')
motor_pair.set_default_speed(20)
colour_c = ColorSensor('C')
distance = DistanceSensor('D')
motor_e = Motor('E')

# create a peripheral object
remote = BLEPeripheral()

# scan for the target device and connect if found
utime.sleep(1)
remote.on_connect(callback=on_connect)
remote.on_disconnect(callback=on_disconnect)
remote.connect()

grabberOpen = True