def __init__(self, leftmotorport, rightmotorport, headmotorport=None):
        """Assumes a three motor puppy configuration
        with two legs and one head motor"""
        self.prime_hub = PrimeHub()
        self.prime_hub.light_matrix.show_image('HAPPY')
        self.legR = Motor(rightmotorport)  # right leg
        self.legL = Motor(leftmotorport)  # left leg

        #self.legs = DriveBase(self.legL, self.legR, 30, 80)
        if headmotorport is not None:
            # If no headmotorport is given, it is not assigned.
            self.head = Motor(headmotorport)  # head motor
        else:
            self.head = None
示例#2
0
文件: M02.py 项目: Niko-bru/LegoSpike
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_front_motor = Motor('F')
g_front_motor.set_default_speed(30)
g_back_motor = Motor('C')
g_back_motor.set_default_speed(30)
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')


def to_mission(hub, motor_pair, front_motor, back_motor):
    motor_pair.move(83, 'cm', steering=0, speed=25)


def do_mission(hub, motor_pair, front_motor, back_motor):
    i = 0
    while i < 41:
        motor_pair.move(0.5, 'cm', steering=0, speed=35)
        print(i)
        i += 1


def go_home(hub, motor_pair, front_motor, back_motor):
    motor_pair.move_tank(100, 'cm', -65, -65)
示例#3
0
# This example finds and connects to a peripheral running the
# UART service (e.g. ble_simple_peripheral.py).
from spike import PrimeHub
from spike import Motor

# Do something
hubprime = PrimeHub()
import hub

motor_drive = Motor("B")
motor_steer = Motor("A")

motor_steer.run_to_position(17)

import bluetooth
import random
import struct
import time
import micropython
import ubinascii


def light(n):
    x = n % 5
    y = n // 5
    hubprime.light_matrix.set_pixel(x, y)


from micropython import const

_IRQ_CENTRAL_CONNECT = const(1 << 0)
示例#4
0

def printbuf():
    global buf
    print(buf)
    buf = ""


# define motors and use brake mode


def waitformotor(motor):
    xxx = 0


paper = Motor("E")
pen1 = Motor("B")
#pen2 = ev3.MediumMotor('outD')
head = Motor("D")

pen1.set_stop_action("brake")
#pen2.stop_action = "brake"
head.set_stop_action("brake")
paper.set_stop_action("brake")
head.set_degrees_counted(0)
pen1.set_degrees_counted(0)
#pen2.reset()
paper.set_degrees_counted(0)

#move paper until color sensor recieves >50 reading
示例#5
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() 
color_sensor_E = ColorSensor('E')
color_sensor_F = ColorSensor('F')

motor_A =  Motor('A') # Set the motor port to the motor.
motor_B = Motor('B') # Set the motor port to the motor.

motor_A.set_default_speed(-30) # Set the default speed of the motor.
motor_B.set_default_speed(30) # Set the default speed of the motor.

motor_A.set_stop_action('brake') # Activate the brakes when the motor stops. The other conditions are 'hold' and 'coast'.
motor_B.set_stop_action('brake') # Activate the brakes when the motor stops.

motor_A_flag = 0 # Create a flag for motor A and set it to OFF.
motor_B_flag = 0 # Create a flag for motor B and set it to OFF.

def stop_at(color):
    motor_A_flag = 0 # Reset the flag for motor A to OFF.
    motor_B_flag = 0 # Reset the flag for motor B to OFF.

    # Move forward.
    motor_A.start()
    motor_B.start()

    while (motor_A_flag == 0) or (motor_B_flag == 0): # Repeat while both sensors ever detect black color.
        if color_sensor_E.get_color() == color: # If the color sensor on port E detect the desired color
            motor_A.stop() # stop the motor an port A and
from spike import Motor
from spike import PrimeHub

hub = PrimeHub()
motor = Motor('A')

# Start the motor
motor.start()

# Wait until left button is pressed
hub.left_button.wait_until_pressed()
print("Left button pressed")

# Stop the motor
motor.stop()
class PuppyBasic:
    "PuppyBasic class with basic Puppy actions"

    def __init__(self, leftmotorport, rightmotorport, headmotorport=None):
        """Assumes a three motor puppy configuration
        with two legs and one head motor"""
        self.prime_hub = PrimeHub()
        self.prime_hub.light_matrix.show_image('HAPPY')
        self.legR = Motor(rightmotorport)  # right leg
        self.legL = Motor(leftmotorport)  # left leg

        #self.legs = DriveBase(self.legL, self.legR, 30, 80)
        if headmotorport is not None:
            # If no headmotorport is given, it is not assigned.
            self.head = Motor(headmotorport)  # head motor
        else:
            self.head = None

    def sit(self):
        """ Puppy sit with leg motors"""
        # TODO: modify to use paired motors
        self.legR.start_at_power(75)
        self.legL.start_at_power(75)
        wait_for_seconds(.4)
        self.legR.start_at_power(0)
        self.legL.start_at_power(0)
        print("Sitting Down")

    def stand(self):
        """ Puppy stand with leg motors"""
        # TODO: modify to use paired motors
        self.legR.start_at_power(-75)
        self.legL.start_at_power(-75)
        wait_for_seconds(.4)
        self.legR.start_at_power(0)
        self.legL.start_at_power(0)
        print("Standing Up")

    def bark(self):
        """Play bark sound"""
        self.prime_hub.speaker.beep(69, 0.5)

    def snore(self):
        """Play snore sound"""
        self.prime_hub.speaker.beep(60, 0.5)

    def happy(self):
        """Happy behavior
        Assumes puppy is seated"""

        self.self.prime_hub.light_matrix.show_image('MEH')
        for value in range(1, 4):
            #self.legs.drive(-100, 0)
            self.bark()
            wait_for_seconds(.2)
            self.legs.stop()
            wait_for_seconds(.3)
            #self.legs.drive(10, 0)
            wait_for_seconds(.3)
            self.legs.stop()
        self.self.prime_hub.light_matrix.show_image('HAPPY')

    def sleepy(self):
        """Sleepy behavior
        Assumes puppy is seated"""
        self.prime_hub.light_matrix.show_image('ASLEEP')
        self.head.run_time(900, 3000)
        for value in range(1, 3):
            self.snore()
        self.head.run_time(-900, 3000)
        self.prime_hub.light_matrix.show_image('HAPPY')
示例#8
0
from spike import Motor

motor = Motor('A')

# Run clockwise for half a second at 75% speed
motor.run_for_seconds(0.5, 75)

# Run counterclockwise for 6 seconds at 30% speed
motor.run_for_seconds(6, -30)

# Run the motor 90 degrees clockwise:
#motor.run_for_rotations(0.25)

# Run the motor 90 degrees counterclockwise:
#motor.run_for_rotations(-0.25)

# Set the motor to position 0, aligning the markers
#motor.run_to_position(0)

# Run the motor 90 degrees clockwise
#motor.run_for_degrees(90)

# Run the motor 90 degrees counterclockwise
#motor.run_for_degrees(-90)

# Run the motor 360 degrees clockwise, at maximum speed (100%)
#motor.run_for_degrees(360, 100)
# from pybricks.parameters import (Port, Stop, Direction, Button, Color, SoundFile, ImageFile, Align)
# from hub import (port, stop, direction, button, color, SoundFile, ImageFile, Align)

# from pybricks.tools import print, wait, StopWatch
# from hub import print, wait_for_seconds, StopWatch

# from pybricks.robotics import DriveBase
# import urequests
 
# ev3 = EV3Brick()
hub = PrimeHub()

# Connect motor to port A and touch sensor to port S1
# pointer=Motor(Port.A)
pointer = Motor('A')

# touch=TouchSensor(Port.S1)
 touch = ForceSensor('E')
 
#set the arrow to blue bar (minimum) to begin with
# pointer.reset_angle(0)

pointer.run_to_position(0)
 
# defining the maximum and minimum temperatures shown by the dashboard
# You can change the minimum and maximum temperatures based on your location.
# Calculate where you should place the green (comfortable temperature)
# and yellow (slightly uncomfortable temperature) bars on your design?
 
blue_temp = 0 # minimum temperature in celsius
示例#10
0
from spike import PrimeHub, Motor, ForceSensor
import utime
hub = PrimeHub()
pointer = Motor('A')
touch = ForceSensor('E')
pointer.run_to_position(0)  #Note: not tested yet
blue_temp = 0
red_temp = 40
angle_bw_blueandred = 180


def turn_pointer():
    temp_in_kelvin = 300
    city_name = "tester"
    temp_in_celsius = round(temp_in_kelvin - 273.15)
    print(temp_in_celsius)
    unit_degree = angle_bw_blueandred / (red_temp - blue_temp)
    turn_angle = unit_degree * temp_in_celsius
    pointer.run_for_degrees(20, turn_angle)
    return temp_in_celsius, city_name


while True:
    temp_in_celsius, city_name = turn_pointer()
    if touch.is_pressed():
        hub._light_matrix.write("It's " + str(temp_in_celsius) +
                                "degree celsius in" + city_name)
    utime.sleep(1)
示例#11
0
from spike import Motor
from spike import PrimeHub

hub = PrimeHub()
motor = Motor('A')

# Start the motor at 30% power
motor.start_at_power(30)

# Wait until left button is pressed
hub.left_button.wait_until_pressed()
print("Left button pressed")

# Stop the motor
motor.stop()
示例#12
0
文件: M11.py 项目: Niko-bru/LegoSpike
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

hub = PrimeHub()
motor_pair = MotorPair('A', 'E')
right_motor = Motor('E')
left_color = ColorSensor('B')
right_color = ColorSensor('D')
wheel_distance_apart = 14.5
wheel_radius = 4.25
wheel_circumference = 2 * math.pi * wheel_radius
motor_pair.set_motor_rotation(wheel_circumference, 'cm')
right_motor.set_default_speed(15)

i = 0
motor_pair.start_tank(25, 25)
while i < 200:
    if left_color.get_color() == 'black':
        print('l')
        motor_pair.start_tank(5, 25)
    elif right_color.get_color() == 'black':
        print('r')
        motor_pair.start_tank(25, 5)
    i += 1
motor_pair.stop()
motor_pair.move_tank(165, 'cm', left_speed=15, right_speed=15)
right_motor.run_for_rotations(20)
motor_pair.start_tank(-25, -25)
示例#13
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_front_motor = Motor('F')
g_front_motor.set_default_speed(30)
g_back_motor = Motor('C')
g_back_motor.set_default_speed(15)
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')


def turn(hub, motor_pair, angle_to_turn):
    if angle_to_turn != 0:
        print('AngleToTurn', angle_to_turn)
        fudge = 0.7
        rotate_const = 586.0 / 360.0

        hub.motion_sensor.reset_yaw_angle()
        turn_angle = rotate_const * angle_to_turn

        motor_pair.move(turn_angle, 'degrees', steering=100, speed=10)
        while True:
            yaw_angle = hub.motion_sensor.get_yaw_angle()

            if yaw_angle < 0 and angle_to_turn > 0:
示例#14
0
    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

while remote.is_connected() is not None:
    # when an object detection notification is received, go and pickup the object
    if grabberOpen == True and remote.getMoveData() is not None: