# 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 # SPI from pyb import SPI
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())
ext = ExtInt(Pin('P9'), ExtInt.IRQ_FALLING, Pin.PULL_UP, key) #下降沿触发,打开上拉电阻 ############################################## # OLED初始显示 ############################################## oled.fill(0) # 清屏显示黑色背景 oled.text('01Studio', 0, 0) # 首行显示01Studio oled.text('Servo-360', 0, 15) # 次行显示实验名称 oled.text(str(speed[i]), 0, 35) # 显示当前速度值 oled.text('Pls Press USER', 0, 55) #提示按按键 oled.show() #S1指定速度,启动时i=0,默认-100 s1.speed(speed[i]) while True: if key_node == 1: #按键被按下 i = i + 1 if i == 5: i = 0 key_node = 0 #清空按键标志位 #X1指定角度 s1.speed(speed[i]) #显示当前频率 oled.fill(0) # 清屏显示黑色背景 oled.text('01Studio', 0, 0) # 首行显示01Studio
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 = 130 sensor.reset() # Initialize the camera sensor. sensor.set_pixformat(sensor.GRAYSCALE) # or sensor.RGB565 sensor.set_framesize(sensor.QQVGA) # or sensor.QVGA (or others) clock = time.clock() # Tracks FPS. #s1.pulse_width(2000) # initialize the motor controller with a high signal for a second #pyb.delay(1000) #s1.pulse_width(1000) # return motor controller to low/off throttle while (True): led_control(2) s1.speed(120) pyb.delay(10) clock.tick() # Track elapsed milliseconds between snapshots(). img = sensor.snapshot() # Take a picture and return the image. img.find_edges(image.EDGE_CANNY, threshold=(40, 174)) # Find edges lines = img.find_lines(threshold=40) # Find lines. counter = 0 totalslope = 0 for l in lines: img.draw_line(l, color=(127)) # Draw lines if lines: if (l[2] - l[0]) != 0: # don't allow vertical lines (infinite slope) slope = 0 slope = (l[3] - l[1]) / (l[2] - l[0]) # print ('slope') # print (slope)
def start_face_rendering(self): sensor.reset() # Initialize the camera sensor. sensor.set_pixformat(sensor.GRAYSCALE) # or sensor.GRAYSCALE sensor.set_framesize(sensor.B128X128) # or sensor.QQVGA (or others) sensor.set_windowing((92,112)) sensor.skip_frames(10) # Let new settings take affect. sensor.skip_frames(time = 5000) #等待5s s3 = Servo(3) # servo on position 1 (P7) #将蓝灯赋值给变量led led = pyb.LED(3) # Red LED = 1, Green LED = 2, Blue LED = 3, IR LEDs = 4. #SUB = "s1" NUM_SUBJECTS = 4 #图像库中不同人数,一共6人 NUM_SUBJECTS_IMGS = 17 #每人有20张样本图片 # 拍摄当前人脸。 img = sensor.snapshot() #img = image.Image("singtown/%s/1.pgm"%(SUB)) d0 = img.find_lbp((0, 0, img.width(), img.height())) #d0为当前人脸的lbp特征 img = None pmin = 999999 self.num=0 for s in range(1, NUM_SUBJECTS+1): dist = 0 for i in range(2, NUM_SUBJECTS_IMGS+1): img = image.Image("singtown/s%d/%d.pgm"%(s, i)) d1 = img.find_lbp((0, 0, img.width(), img.height())) #d1为第s文件夹中的第i张图片的lbp特征 dist += image.match_descriptor(d0, d1)#计算d0 d1即样本图像与被检测人脸的特征差异度。 print("Average dist for subject %d: %d"%(s, dist/NUM_SUBJECTS_IMGS)) pmin = self.min(pmin, dist/NUM_SUBJECTS_IMGS, s)#特征差异度越小,被检测人脸与此样本更相似更匹配。 print(pmin) print(self.num) # num为当前最匹配的人的编号。 #TS=3 没戴口罩 if (pmin>5000) & (TS==3): uart.write("-- NO People! --") led.off() if (pmin>5000) & (TS==1): uart.write("-- NO People! --") led.off() if pmin<=5000: if self.num==1: #匹配到了people_One uart.write("People One ") if self.num==2: uart.write("People Two ") if self.num==3: uart.write("People Three ") if self.num==4: uart.write("People New ") led.on() #亮灯 led1.off() time.sleep(3500) #延时1500ms led.off() for i in range(1,460): s3.speed(50) # for continuous rotation servos time.sleep(15) s3.speed(0) time.sleep(1500) for i in range(1,230): s3.speed(-50) time.sleep(15) s3.speed(0)
# Servo Control Example # # This example shows how to use your OpenMV Cam to control servos. import micropython, time from pyb import Servo micropython.alloc_emergency_exception_buf(100) s1 = Servo(1) # P7 #s1.calibration(1000, 1500, 1100) s1.calibration(640, 2420, 1500) s1.speed(-100) time.sleep(2000) print(s1.pulse_width()) print(s1.calibration()) s1.speed(-14) time.sleep(5000) s1.speed(-13) time.sleep(5000) i = -20 while (i < 100): print(i) s1.speed(i) print(s1.pulse_width()) time.sleep(1000) i += 1
class ServoZumo: """ A mini class to drive a Zumo Chassis (POL-1418) + DFRobot Servo motor (DFR0399) """ 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() @property def is_moving(self): return self._moving def stop(self): self._right.speed(0) self._left.speed(0) self._moving = False def speed(self, speed): """ Speed from -100 (backward) to +100 (forward) """ self._right.speed(-1 * speed) self._left.speed(speed) self._moving = True def right(self, speed, time_ms=None): """ Turn right @ given speed. if time_ms is given, it stop after the given time """ self._right.speed(speed) self._left.speed(speed) self._moving = True if time_ms: sleep_ms(time_ms) self.stop() def left(self, speed, time_ms=None): """ Turn left @ given speed. if time_ms is given, it stop after the given time """ self.right(-1 * speed, time_ms)