Example #1
0
    def __init__(self, actuator):
        self.servo = None
        self.stepper = None
        self.pin = actuator['pin']
        self.idle = 0
        self.active = 1
        self.speed = 0
        self.time = 0
        self.delay = 0
        self.actuator = None
        self.sensor = None
        self.trigger = 40000
        self.triggered = False

        if "idle_state" in actuator: self.idle = actuator['idle_state']
        if "active_state" in actuator: self.active = actuator['active_state']
        if "speed" in actuator: self.speed = actuator['speed']
        if "time" in actuator: self.time = actuator['time']
        if "delay" in actuator: self.delay = actuator['delay']
        if 'trigger' in actuator: self.trigger = actuator['trigger']
        if "sensor" in actuator: self.sensor = machine.ADC(actuator['sensor'])

        if not isinstance(self.pin, dict):
            if self.speed:  # Assume servo
                x = int(self.pin[1:]) + 1
                self.servo = Servo(x)

            else:
                self.actuator = machine.Pin(self.pin, machine.Pin.OUT)

        else:
            #TODO Handle Stepper motor
            pass

        self.current_state = self.idle
Example #2
0
 def __init__(self, left_servo, right_servo):
     # This motor requires a timer @ 500 Hz so modify the timer(5) used by Servo from 50 HZ to 500 Hz
     self._left = Servo(left_servo)
     #self.left.calibration(500, 2500, 1500, 2500, 2500)
     self._right = Servo(right_servo)
     #self.right.calibration(500, 2500, 1500, 2500, 2500)
     self._moving = False
     self.stop()
Example #3
0
    def __init__(self, p=0, i=0, d=0, imax=0):
        self.pan = Servo(1)  # P7
        self.tilt = Servo(2)  # P8
        self._kp = float(p)
        self._ki = float(i)
        self._kd = float(d)
        self._imax = abs(imax)
        self._last_derivative = float('nan')
        self.corresponding_angle = 0.5  # per pixel
        self.scan_speed = 240

        self._mode_angle_map = {
            1: 0,
            2: 0,
        }
Example #4
0
 def __init__(self):
     self.vd = ADC(Pin(Pin.cpu.A2))
     self.servo = Servo(4)
     self.curr_angle = self.center_angle = 48
     self.f2b = 0
     self.r1 = Pin(Pin.cpu.C4, Pin.OUT)
     self.r2 = Pin(Pin.cpu.A4, Pin.OUT)
     self.t2 = Timer(2, freq=1000)
     self.l = self.t2.channel(2, Timer.PWM, pin=Pin(Pin.cpu.B3))
     self.l1 = Pin(Pin.cpu.B9, Pin.OUT)
     self.l2 = Pin(Pin.cpu.B0, Pin.OUT)
     self.t8 = Timer(8, freq=1000)
     self.r = self.t8.channel(3, Timer.PWM_INVERTED, pin=Pin(Pin.cpu.B15))
     self.lv = 0
     self.rv = 0
     self.v = 25
     self.countR = self.countL = 0
     self.turn(self.center_angle)
     self.dinit_encoder()
     self.init_encoder()
Example #5
0
def pins_test():
    i2c = I2C(1, I2C.MASTER)
    spi = SPI(2, SPI.MASTER)
    uart = UART(3, 9600)
    servo = Servo(1)
    adc = ADC(Pin.board.X3)
    dac = DAC(1)
    pin = Pin('X4', mode=Pin.AF_PP, af=Pin.AF3_TIM9)
    pin = Pin('Y1', mode=Pin.AF_OD, af=3)
    pin = Pin('Y2', mode=Pin.OUT_PP)
    pin = Pin('Y3', mode=Pin.OUT_OD, pull=Pin.PULL_UP)
    pin.high()
    pin = Pin('Y4', mode=Pin.OUT_OD, pull=Pin.PULL_DOWN)
    pin.high()
    pin = Pin('X18', mode=Pin.IN, pull=Pin.PULL_NONE)
    pin = Pin('X19', mode=Pin.IN, pull=Pin.PULL_UP)
    pin = Pin('X20', mode=Pin.IN, pull=Pin.PULL_DOWN)
    print('===== output of pins() =====')
    pins()
    print('===== output of af() =====')
    af()
# Servo Control Example
#
# This example shows how to use your OpenMV Cam to control servos.

import time
from pyb import Servo

s1 = Servo(1) # P7
s2 = Servo(2) # P8

while(True):
    for i in range(1000):
        s1.pulse_width(1000 + i)
        s2.pulse_width(1999 - i)
        time.sleep(10)
    for i in range(1000):
        s1.pulse_width(1999 - i)
        s2.pulse_width(1000 + i)
        time.sleep(10)
Example #7
0
# Servo Control Example
# This example shows how to use your OpenMV Cam to control servos.
# Hardware : Servo, OpenMV
# connect:
#     Servo    OpenMV
#     VCC       5V
#     GND       GND
#     data1     P7
#     data2     P8

import time
from pyb import Servo, Pin

s1 = Servo(1)  # P7
s2 = Servo(2)  # P8

while (True):
    s1.angle(0)
    s2.angle(0)
    time.sleep(1000)
    s1.angle(90)
    s2.angle(90)
    time.sleep(1000)
    s1.angle(0)
    s2.angle(0)
    time.sleep(1000)
    s1.angle(-90)
    s2.angle(-90)
    time.sleep(1000)
Example #8
0
from pyb import Servo

servo = Servo(1)
print(servo)

servo.angle(0)
servo.angle(10, 100)

servo.speed(-10)
servo.speed(10, 100)

servo.pulse_width(1500)
print(servo.pulse_width())

servo.calibration(630, 2410, 1490, 2460, 2190)
print(servo.calibration())
Example #9
0
File: main.py Project: wumode/boat
import sensor, image, time
import time
from pyb import UART
from pyb import Servo
from pyb import Pin

laser_disable=0
laser_enable=1
uart = UART(3, 921600, timeout=50) #串口初始化定义 p4 p5
# red_threshold  =(0, 100, 17, 127, -128, 127)  # 以前
red_threshold  = (20, 68, 41, 127, -13, 54)
yellow_threshold  = red_threshold
#白天 室外 (0, 100, 24, 127, -128, 127) 曝光值 0.6
#室内 灯光 (0, 100, 17, 127, -128, 127) 曝光值 1.5
up_servo=Servo(1)  #p7 p8
down_servo=Servo(2)
up_servo_zero_position = 1500
down_servo_zero_position = 1440
#up_servo_zero_position = 1800       #向下正
#down_servo_zero_position = 1100     #向左正
up_servo.pulse_width(up_servo_zero_position)
down_servo.pulse_width(down_servo_zero_position)

kp_up = -2.6
ki_up = -0.42
kd_up = -0.43

kp_down = 3.19
ki_down = 0.46
kd_down = 0.43
Example #10
0
# In this example we are going use Servo and Slider by reading using the ADC
# Make sure you have the Servo checkbox marked!
# Make sure you have the ADC checkbox marked!
# use the reset button to stop example

from machine import Pin
from pyb import ADC, Servo
from time import sleep_ms

# The slider is connected to pin Y4, try adjusting it
slider = ADC(Pin('Y4'))

# The pyboard has four simple servo connections
servo = Servo(1)

# calculate the basic step
STEP = 255 / 180
last = -1

print("Servo and Slider demo started..")

while True:
    pos = slider.read()

    if (pos != last):
        angle = int((pos / STEP) - 90)  # get the angle
        print("Slider position: ", pos, " Servo angle: ", angle)
        servo.angle(angle, 100)  # set servo angle
        last = pos

    sleep_ms(10)
Example #11
0
#颜色识别内容
import sensor, image, time, math, pyb
from pyb import UART
from pyb import Servo

s1 = Servo(1) #P7 舵机1
s1.pulse_width(1900) #舵机1初始化到中间位置

#设置颜色识别阈值
green_threshold = (8, 20, -22, -9, -4, 19)#(22, 46, -49, -20, -33, 24)(11, 83, -43, -25, -2, 26)
white_threshold = (38, 47, -7, 7, -5, 5)
GRAYSCALE_THRESHOLD = [(13, 69)]


#发送的数据种类选择·
X_Size = 200
H_Size = 210

#找到最大的那块
def find_max(blobs):
    max_size=0
    for blob in blobs:
        if blob[2]*blob[3]>max_size:
            max_blob = blob
            max_size = blob[2]*blob[3]
    return max_blob

#限幅函数
def XianFu(Data1,Data2):
    biggest = 1500
    if(Data1>biggest):
Example #12
0
import time
from pyb import (USB_VCP, Servo)

SLEEP = 0.1

UP = -90
DOWN = 0
SPEED = 1000

servo = Servo(1)
servo.angle(30)

p = USB_VCP()

while True:
    if p.any():
        command = p.readline().strip()
        if command == b'up':
            servo.angle(UP, SPEED)
            p.write(b'up-ok\n')
        elif command == b'down':
            servo.angle(DOWN, SPEED)
            p.write(b'down-ok\n')
        else:
            p.write(command + b'\n')
            p.write(b'err\n')

    time.sleep(SLEEP)
Example #13
0
版本:v1.0
日期:2019.9
作者:01Studio
说明:通过USER按键让让舵机旋转不同角度。
'''

#导入相关模块
from pyb import Servo, ExtInt
from machine import Pin, I2C
from ssd1306x import SSD1306_I2C

#初始化相关模块
i2c = I2C(sda=Pin("P0"), scl=Pin("P2"), freq=80000)
oled = SSD1306_I2C(128, 64, i2c, addr=0x3c)

s1 = Servo(1)  #构建舵机对象s1,输出引脚为X1

#定义5组角度:-90、-45、0、45、90
angle = [-90, -45, 0, 45, 90]

key_node = 0  #按键标志位
i = 0  #用于选择角度


##############################################
#  按键和其回调函数
##############################################
def key(ext):
    global key_node
    key_node = 1
Example #14
0
# Autonomous rover code by Chris Anderson, 3DR, DIY Robocars
# Copyrigbt 2017
# Released under a BSD 2.0 Open Source Licence

import sensor, image, pyb, math, time
from pyb import Servo
from pyb import LED

s1 = Servo(1) # P7 Motor
s2 = Servo(2) # P8 Steering
print (s1.calibration()) # show throttle servo calibration
cruise_speed = 0 # how fast should the car drive, range from 1000 to 2000
steering_gain = 500
kp = 0.8   # P term of the PID
ki = 0     # I term of the PID
kd = 0     # D term of the PID

red_led   = LED(1)
green_led = LED(2)
blue_led  = LED(3)
ir_led    = LED(4)

old_error = 0
measured_angle = 0
set_angle = 90 # this is the desired steering angle (straight ahead)
p_term = 0
i_term = 0
d_term = 0
old_time = pyb.millis()

def led_control(x):
Example #15
0
from pyb import Servo

servo = Servo(1)
print(servo)

servo.angle(0)
servo.angle(10, 100)

servo.speed(-10)
servo.speed(10, 100)

servo.pulse_width(1500)
print(servo.pulse_width())

servo.calibration(630, 2410, 1490, 2460, 2190)
print(servo.calibration())
Example #16
0
Create date: 3/15/2016   Last modified date: 3/30/2016
from pyb import Servo
'''


import pyb
from pyb import Pin
from pyb import Servo
from pyb import LED

l1=LED(1)
l2=LED(2)
l3=LED(3)
l4=LED(4)
# creat servo projects.
s1 = Servo(1)
s2 = Servo(2) 
s3 = Servo(3)
#s4 = Servo(4)
# go to straight line
'''
s1.angle(0,2000)
pyb.delay(1000)  
s2.angle(0,2000)
pyb.delay(1000)
s3.angle(0,2000)
pyb.delay(1000)


# Movement 1
s1.angle(-50,2000)
Example #17
0
import sensor, image, time, math

import pyb, ustruct
import time
from pyb import Servo

#P4 -> 21
#P5->20

s1 = Servo(1)  # P7
s2 = Servo(2)  # P8

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time=2000)
sensor.set_brightness(0)
sensor.set_contrast(0)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
clock = time.clock()

tag_families = 0
tag_families |= image.TAG16H5

s1.pulse_width(1450)

s2.pulse_width(1200)


def family_name(tag):
Example #18
0
# AprilTags Example
#
# This example shows the power of the OpenMV Cam to detect April Tags
# on the OpenMV Cam M7. The M4 versions cannot detect April Tags.

#库导入
import sensor, image, time, math
import json
from pyb import Servo
from pyb import UART
uart = UART(3, 9600) #串口输出设置
#相机初始化
s1 = Servo(1) # P7 Servo
s2 = Servo(2) # P8 Servo
ss1=90
ss2=90
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.HQVGA) # we run out of memory if the resolution is much bigger...
sensor.skip_frames(30)
sensor.set_auto_gain(True)  # must turn this off to prevent image washout...
sensor.set_auto_whitebal(False)  # must turn this off to prevent image washout...
clock = time.clock()
#相机分辨率矫正
f_x = (2.8 / 3.984) * 160 # 默认值
f_y = (2.8 / 2.952) * 120 # 默认值
c_x = 240 * 0.5 # 默认值(image.w * 0.5)
c_y = 160 * 0.5 # 默认值(image.h * 0.5)
def degrees(radians):
    return (180 * radians) / math.pi
while(True): #主循环
Example #19
0
# 设计:沈阳航空航天大学 berry

# 博客:https://blog.csdn.net/qq_45037925/article/details/105901893

# 交流方式:私信我的博客即可

import time

from pyb import Servo
import sensor, image, time

from pyb import UART

#导入需要的模块

s1 = Servo(1)  # P7

s2 = Servo(2)  # P8

s3 = Servo(3)  # P9 Only for OpenMV3 M7
#实例化3个舵机

uart = UART(3, 115200)  #初始化串口

s2_now = -90
s3_now = 0

red_threshold = (21, 100, 36, 127, -9, 34
                 )  #颜色阈值1   0001(14, 100, 8, 127, 9, 116)
blue_threshold = ((20, 67, -52, -13, -36, -1))  #颜色阈值2   0010
yellow_threshold = (14, 100, 22, 127, 42, 113)  #颜色阈值4    0100
Example #20
0
from pyb import Servo
from objects import RCmotors

print("tests:")
print("- (s)ervo")
print("- (m)otor")

servo = Servo(1)
motors = RCmotors(0)

motors.speed(0)

test_name = input("test to run: ")


if test_name == "s":
    while True:
        value = input("> ")
        if value == 'r':
            servo.angle(-12)
        elif value == 'l':
            servo.angle(14)
        elif value == 'f':
            servo.angle(2)
        else:
            try:
                servo.angle(int(value))
            except ValueError:
                pass
elif test_name == "m":
    while True:
版本:v1.0
日期:2019.9
作者:01Studio
说明:通过USER按键让舵机实现不同速度旋转。
'''

#导入相关模块
from pyb import Servo, ExtInt
from machine import Pin, I2C
from ssd1306x import SSD1306_I2C

#初始化相关模块
i2c = I2C(sda=Pin("P0"), scl=Pin("P2"), freq=80000)
oled = SSD1306_I2C(128, 64, i2c, addr=0x3c)

s1 = Servo(1)  #构建舵机对象s1,输出引脚为X1

#定义5组速度值
speed = [-100, -50, 0, 50, 100]

key_node = 0  #按键标志位
i = 0  #用于选择角度


##############################################
#  按键和其回调函数
##############################################
def key(ext):
    global key_node
    key_node = 1
Example #22
0
class MyServo:

    _kp = _ki = _kd = _integrator = _imax = 0
    _last_error = _last_derivative = _last_t = 0
    _RC = 1 / (2 * pi * 20)

    def __init__(self, p=0, i=0, d=0, imax=0):
        self.pan = Servo(1)  # P7
        self.tilt = Servo(2)  # P8
        self._kp = float(p)
        self._ki = float(i)
        self._kd = float(d)
        self._imax = abs(imax)
        self._last_derivative = float('nan')
        self.corresponding_angle = 0.5  # per pixel
        self.scan_speed = 240

        self._mode_angle_map = {
            1: 0,
            2: 0,
        }

    def init(self, mode=1, tilt_angle=0):
        # self.pan.angle(self._mode_angle_map[mode])
        self.tilt.angle(tilt_angle)

    def get_pid(self, error, scaler):
        tnow = millis()
        dt = tnow - self._last_t
        output = 0
        if self._last_t == 0 or dt > 1000:
            dt = 0
            self.pid_clear()
        self._last_t = tnow
        delta_time = float(dt) / float(1000)
        output += error * self._kp
        if abs(self._kd) > 0 and dt > 0:
            if isnan(self._last_derivative):
                derivative = 0
                self._last_derivative = 0
            else:
                derivative = (error - self._last_error) / delta_time
            derivative = self._last_derivative + (
                (delta_time / (self._RC + delta_time)) *
                (derivative - self._last_derivative))
            self._last_error = error
            self._last_derivative = derivative
            output += self._kd * derivative
        output *= scaler
        if abs(self._ki) > 0 and dt > 0:
            self._integrator += (error * self._ki) * scaler * delta_time
            if self._integrator < -self._imax:
                self._integrator = -self._imax
            elif self._integrator > self._imax:
                self._integrator = self._imax
            output += self._integrator
        return output

    def pid_clear(self):
        self._integrator = 0
        self._last_derivative = float('nan')

    def rotate_steering_gear(self, delta_pixel):
        delta_pixel = -delta_pixel
        pan_output = self.get_pid(delta_pixel, 1) / 2
        self.pan.angle(self.pan.angle() + pan_output)
        return self.pan.angle()

    def scan(self, count):
        parity = 0
        if count % 2 == 0:
            if parity == 1:
                self.pid_clear()
            parity = 0
            self.rotate_steering_gear(self.scan_speed)
        elif count % 2 == 1:
            if parity == 0:
                self.pid_clear()
            parity = 1
            self.rotate_steering_gear(-self.scan_speed)
        return self.pan.angle()
import sensor, image, time

from pid import PID
from pyb import Servo

pan_servo=Servo(1)
tilt_servo=Servo(2)

pan_servo.calibration(500,2500,500)
tilt_servo.calibration(500,2500,500)

red_threshold  = (13, 49, 18, 61, 6, 47)

pan_pid = PID(p=0.07, i=0, imax=90) #脱机运行或者禁用图像传输,使用这个PID
tilt_pid = PID(p=0.05, i=0, imax=90) #脱机运行或者禁用图像传输,使用这个PID
#pan_pid = PID(p=0.1, i=0, imax=90)#在线调试使用这个PID
#tilt_pid = PID(p=0.1, i=0, imax=90)#在线调试使用这个PID

sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.GRAYSCALE) # use RGB565.
sensor.set_framesize(sensor.QVGA) # use QQVGA for speed.
sensor.set_vflip(True)
sensor.skip_frames(10) # Let new settings take affect.
sensor.set_auto_whitebal(False) # turn this off.
clock = time.clock() # Tracks FPS.
face_cascade = image.HaarCascade("frontalface", stages=25)

def find_max(blobs):
    max_size=0
    for blob in blobs:
        if blob[2]*blob[3] > max_size:
sleep(1)

led.fill(blue)
led.write()
sleep(1)

led.fill((255, 0, 255))  # Magenta
led.write()
sleep(1)

led.fill((0, 0, 0))  # Black
led.write()
lcd.println('ok')

lcd.print("Servo:")
servo1 = Servo(1)  # from -90 to +90 angle. At 0 degree at initialisation
servo2 = Servo(2)
servo3 = Servo(3)
servo4 = Servo(4)

# Create a list of sevo for convenience
servo_list = [servo1, servo2, servo3, servo4]

print("All servos at -90")
for servo in servo_list:
    servo.angle(-90)
    sleep(0.5)

print("All servos at +90")
for servo in servo_list:
    servo.angle(+90)
Example #25
0
import sensor, image, time
import network, usocket, sys

#import utils
from pyb import UART
from pyb import LED
from pyb import Servo
from pyb import Pin, Timer

blue_led  = LED(3)
green_led = LED(2)
red_led   = LED(1)

sx = Servo(1) # P7
sy = Servo(2) # P8
gainx = -5
gainy = -5

uart = UART(3, 9600)
uart.init(9600, bits=8, parity=None, stop=1)

EXPOSURE_TIME_SCALE = 0.05
thresholds = (65, 255)
roi1 = (0,0,320,240)
tim = Timer(4, freq=1)

def find_max(blobs):
    max_size=0
    for blob in blobs:
        if blob.pixels() > max_size:
            max_blob=blob
Example #26
0
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)
sensor.set_auto_gain(False)  # Must be turned off for color tracking
sensor.set_auto_whitebal(False)  # Must be turned off for color tracking
clock = time.clock()

# Only blobs that with more pixels than "pixel_threshold" and more area than "area_threshold" are
# returned by "find_blobs" below. Change "pixels_threshold" and "area_threshold" if you change the
# camera resolution. Don't set "merge=True" becuase that will merge blobs which we don't want here.

####################################################################
# Servo initialization and calibration here

pan = Servo(1)  # use PWM on P7, s2 on shield
pan.calibration(540, 2400, 540, 1400, 1500)
pan.angle(90)

tilt = Servo(2)  # use PWM on P8, s1 on shield
tilt.calibration(540, 2400, 1200, 1800, 1500)
tilt.angle(90)

delay(1000)  # delay to give servos time to get to 90 & 90
#####################################################################

#####################################################################
# DC Motor pin setup

LEFT_MOTOR1 = Pin('P0', Pin.OUT_PP, Pin.PULL_NONE)
LEFT_MOTOR2 = Pin('P1', Pin.OUT_PP, Pin.PULL_NONE)
Example #27
0
p_in = Pin('X2', Pin.IN, Pin.PULL_UP)
p_in.value() # get value, 0 or 1

# pin PWM control
from pyb import Pin, Timer

p = Pin('X1') # X1 has TIM2, CH1
tim = Timer(2, freq=1000)
ch = tim.channel(1, Timer.PWM, pin=p)
ch.pulse_width_percent(50)

# servo control
from pyb import Servo

s1 = Servo(1) # servo on position 1 (X1, VIN, GND)
s1.angle(45) # move to 45 degrees
s1.angle(-60, 1500) # move to -60 degrees in 1500ms
s1.speed(50) # for continuous rotation servos

# ADC/DAC
from pyb import Pin, ADC

adc = ADC(Pin('X19'))
adc.read() # read value, 0-4095

from pyb import Pin, DAC

dac = DAC(Pin('X5'))
dac.write(120) # output between 0 and 255
Example #28
0
from pyb import Accel, Servo
servo = Servo(1)
accel = Accel()
while 1:
    servo.angle(accel.x(), 100)
    pyb.delay(200)
Example #29
0
class Actuator:
    def __init__(self, actuator):
        self.servo = None
        self.stepper = None
        self.pin = actuator['pin']
        self.idle = 0
        self.active = 1
        self.speed = 0
        self.time = 0
        self.delay = 0
        self.actuator = None
        self.sensor = None
        self.trigger = 40000
        self.triggered = False

        if "idle_state" in actuator: self.idle = actuator['idle_state']
        if "active_state" in actuator: self.active = actuator['active_state']
        if "speed" in actuator: self.speed = actuator['speed']
        if "time" in actuator: self.time = actuator['time']
        if "delay" in actuator: self.delay = actuator['delay']
        if 'trigger' in actuator: self.trigger = actuator['trigger']
        if "sensor" in actuator: self.sensor = machine.ADC(actuator['sensor'])

        if not isinstance(self.pin, dict):
            if self.speed:  # Assume servo
                x = int(self.pin[1:]) + 1
                self.servo = Servo(x)

            else:
                self.actuator = machine.Pin(self.pin, machine.Pin.OUT)

        else:
            #TODO Handle Stepper motor
            pass

        self.current_state = self.idle

    def isActivated(self):
        return self.current_state == self.active

    def triggerActuator(self, value):
        try:
            if isinstance(self.pin, dict):
                print("TODO: triggerActuator handle stepper motors")
                pass

            else:
                if self.servo:
                    self.servo.angle(value, self.speed)

                else:
                    self.actuator.value(value)

        except Exception as e:
            sys.print_exception(e)

    def activate(self):
        print("Activating %s: %s" % (self.pin, self.active))
        self.triggerActuator(self.active)
        self.current_state = self.active

        if not isinstance(self.pin, dict) and not self.sensor:
            print("time: %i" % self.time)
            time.sleep_ms(self.time)

        if not self.sensor:
            self.deactivate()
            print("delay: %i" % self.delay)
            time.sleep_ms(self.delay)
        else:
            pass

    def deactivate(self):
        print("Deativating %s: %s" % (self.pin, self.idle))
        self.triggerActuator(self.idle)
        self.current_state = self.idle

    def readSensor(self):
        if self.isActivated():
            sensor_value = self.sensor.read_u16()
            self.triggered = sensor_value >= self.trigger
            print(self.sensor, self.triggered, sensor_value)

        return self.triggered
Example #30
0
# Servo Control Example
#
# This example shows how to use your OpenMV Cam to control servos.

import time
from pyb import Servo

s1 = Servo(1)  # P7
s2 = Servo(2)  # P8

while (True):
    for i in range(1000):
        s1.pulse_width(1000 + i)
        s2.pulse_width(1999 - i)
        time.sleep(10)
    for i in range(1000):
        s1.pulse_width(1999 - i)
        s2.pulse_width(1000 + i)
        time.sleep(10)
# main.py -- put your code here!
import pyb  # importing the board
# move servo based on the movement of the board
from pyb import Servo
accel = pyb.Accel()  # assigning the sensor to act as movement detector…
ss = Servo(1)  # servo on position 1 (X1, VIN, GND)
while True:  # while loop
    if accel.x() > 5:
        ss.angle(90)
    elif accel.x() < -5:
        ss.angle(-90)
    elif accel.x() == 0:
        ss.angle(0)
Example #32
0
import sensor, image, time

from pid import PID
from pyb import Servo

pan_servo = Servo(1)
tilt_servo = Servo(2)

red_threshold = (13, 49, 18, 61, 6, 47)

pan_pid = PID(p=0.07, i=0, imax=90)  #脱机运行或者禁用图像传输,使用这个PID
tilt_pid = PID(p=0.05, i=0, imax=90)  #脱机运行或者禁用图像传输,使用这个PID
#pan_pid = PID(p=0.1, i=0, imax=90)#在线调试使用这个PID
#tilt_pid = PID(p=0.1, i=0, imax=90)#在线调试使用这个PID

sensor.reset()  # Initialize the camera sensor.
sensor.set_pixformat(sensor.GRAYSCALE)  # use RGB565.
sensor.set_framesize(sensor.QVGA)  # use QQVGA for speed.
sensor.set_vflip(True)  #图像镜像 垂直翻转
sensor.skip_frames(10)  # Let new settings take affect.
sensor.set_auto_whitebal(False)  # turn this off.
clock = time.clock()  # Tracks FPS.
face_cascade = image.HaarCascade("frontalface", stages=25)


def find_max(blobs):
    max_size = 0
    for blob in blobs:
        if blob[2] * blob[3] > max_size:
            max_blob = blob
            max_size = blob[2] * blob[3]
Example #33
0
# Test the 4 servo ouputs labelled SERVO1, SERVO2, SERVO3 & SERVO4
from pyb import Servo
from time import sleep
servo1 = Servo(1) # from -90 to +90 angle. At 0 degree at initialisation
servo2 = Servo(2)
servo3 = Servo(3)
servo4 = Servo(4)

# Create a list of sevo for convenience
servo_list = [ servo1,servo2, servo3, servo4 ]

print( "All servos at -90" )
for servo in servo_list:
	servo.angle( -90 )
	sleep( 0.5 )

print( "All servos at +90" )
for servo in servo_list:
	servo.angle( +90 )
	sleep( 0.5 )

print( "All servos at 0 degree (at once)" )
for servo in servo_list:
	servo.angle( 0 )

print( "Wait 2 seconds")
sleep( 2 )

print( "coordinate servo movement" )
# Coordonate move to -90° for servo1 & servo2 during 2 seconds
# while coordinate move to +90° for servo3 & servo4 during the same 2 seconds.
Example #34
0
import sensor, image, time
from pyb import Pin
from pyb import Servo

tile_servo = Servo(1)  #上
pan_servo = Servo(2)  #下
pan_servo.angle(-2)
pan_servo.angle(0)
s = Pin('P5', Pin.IN, Pin.PULL_DOWN)
s_8 = Pin('P1', Pin.OUT_PP)
s_4 = Pin('P2', Pin.OUT_PP)
s_2 = Pin('P3', Pin.OUT_PP)
s_1 = Pin('P4', Pin.OUT_PP)

red_threshold = (18, 55, 23, 90, -3, 80)
k = 2479
list_num = []
data = 0
i = 0
fire = 0
num = 0
sensor.reset()  # Initialize the camera sensor.
sensor.set_pixformat(sensor.RGB565)  # use RGB565.
sensor.set_framesize(sensor.QQVGA)  # use QQVGA for speed.
sensor.skip_frames(10)  # Let new settings take affect.
sensor.set_auto_whitebal(False)  # turn this off.
clock = time.clock()  # Tracks FPS.

s.low()
s_8.low()
s_4.low()