Example #1
0
import time
from pyb import Pin, SPI
#lcd引脚对应接口
# SCL    P2
# SDA    P0
# CS     P3
# DC/RS  P8
# RESET  P7
cs = Pin("P3", Pin.OUT_OD)
rst = Pin("P7", Pin.OUT_PP)
rs = Pin("P8", Pin.OUT_PP)
# The hardware SPI bus for your OpenMV Cam is always SPI bus 2.
spi = SPI(2, SPI.MASTER, baudrate=int(1000000000 / 66), polarity=0, phase=0)


def write_command_byte(c):
    cs.low()
    rs.low()
    spi.send(c)
    cs.high()


def write_data_byte(c):
    cs.low()
    rs.high()
    spi.send(c)
    cs.high()


def write_command(c, *data):
    write_command_byte(c)
Example #2
0
import nrf24l01use
from pyb import Pin

nrf=nrf24l01use.nrf24l01(spi=1,csn='X5',ce='X4')
i=0
sw=Pin('X17',Pin.IN)
# while True:
	# if(sw.value()==0):
		# while sw.value()==0:
			# a=0
		# nrf.master(2)
		# print('ok')

while True:
	  nrf.master('A:%s'%i)							 #发送内容
	  i+=1
	  if(i==10):
		i=0
Example #3
0
import sensor, image, time

# 为了使色彩追踪效果真的很好,你应该在一个非常受控制的照明环境中。
black_threshold = (0, 25, -2, 24, -128, 55)
#设置绿色的阈值,括号里面的数值分别是L A B 的最大值和最小值(minL, maxL, minA,
# maxA, minB, maxB),LAB的值在图像左侧三个坐标图中选取。如果是灰度图,则只需
#设置(min, max)两个数字即可。

# You may need to tweak the above settings for tracking green things...
# Select an area in the Framebuffer to copy the color settings.
tim1 = Timer(2, freq=1000)  # Frequency in Hz
tim2 = Timer(4, freq=1000)  # Frequency in Hz
# tim3 = Timer(1, freq=1000) # Frequency in Hz
# 生成1kHZ方波,使用TIM4,channels 1 and 2分别是 50% 和 75% 占空比。

PWM1 = tim1.channel(3, Timer.PWM, pin=Pin("P4"))
PWM2 = tim1.channel(4, Timer.PWM, pin=Pin("P5"))
PWM3 = tim1.channel(1, Timer.PWM, pin=Pin("P6"))
PWM4 = tim2.channel(1, Timer.PWM, pin=Pin("P7"))
PWM5 = tim2.channel(2, Timer.PWM, pin=Pin("P8"))
PWM6 = tim2.channel(3, Timer.PWM, pin=Pin("P9"))

PWM1.pulse_width_percent(0)
PWM2.pulse_width_percent(0)
PWM3.pulse_width_percent(0)
PWM4.pulse_width_percent(0)
PWM5.pulse_width_percent(0)
PWM6.pulse_width_percent(0)

sensor.reset()  # Initialize the camera sensor.
sensor.set_pixformat(sensor.RGB565)  # use RGB565.
# Charge d'un condensateur à travers une résistance
# R = 100k et C = 220 nF

from pyb import Pin, ADC, Timer, ExtInt
from time import sleep_ms
import array

T = 1  # millisecond
f = 10**3 / T
nb = 100

pinX1 = Pin('X1', Pin.OUT)  # Alimentation du circuit RC
pinX2 = Pin('X2')  # Tension condensateur
adc = ADC(pinX2)  # Activation du CAN

buf = array.array("h", nb * [0x7FFF])  # h = signed short (int 2 octets)
tim = Timer(6, freq=f)  # create a timer running at 10Hz
mesures = lambda e: adc.read_timed(buf, tim)  # read analog values into buf
ext = ExtInt(Pin('X3'), ExtInt.IRQ_FALLING, Pin.PULL_NONE, mesures)

pinX1.low()  # Décharge du condensateur
sleep_ms(1000)  # Attendre 2 s
pinX1.high()  # Début de la charge
sleep_ms(1000)
ext.disable()

x = [i * 1 / f * 1000 for i in range(nb)]
y = [val for val in buf]
for i in range(len(x)):
    if y[i] < (max(y) * 0.37):
        tau = x[i]
Example #5
0
print('Performing Milestone 2')
print('Waiting for button press')


while not trigger():		# Wait for trigger press
	time.sleep(0.001)
while trigger(): pass
# Wait for release
print('Button pressed - Running')
oled.draw_text(0, 30, 'Button pressed - Running')
oled.display()

# define ports for microphone, LEDs and trigger out (X5)

mic = ADC(Pin('Y11'))

MIC_OFFSET = 1527		# ADC reading of microphone for silence

dac = pyb.DAC(1, bits=12)  # Output voltage on X5 (BNC) for debugging

b_LED = LED(4)		# flash for beats on blue LED



N = 160				# size of sample buffer s_buf[]

s_buf = array('H', 0 for i in range(N))  # reserve buffer memory

ptr = 0				# sample buffer index pointer
Example #6
0
#Light control module.
#Use led on board simulate the bulb.
#Usr can turn on & turn off the led, besides, they can control lightness of led.
#Because succulent need to be shined regularly.
def Turn_On(num):
    light = LED(num)
    light.On()


def Turn_Off(num):
    light = LED(num)
    light.Off()


#Adjust lightness of led.
def Lightness(par):
    pwm_control


import pyb
from pyb import Pin
pin_out = Pin('Y2', Pin.OUT_PP)


def light_on():
    pin_out.high()


def light_off():
    pin_out.low()
Example #7
0
from pyb import Pin
import time

key0 = Pin("P6", Pin.IN)
key1 = Pin("P9", Pin.IN)
key2 = Pin("P10", Pin.IN)

key_last = 0
cnt = 0
flag = 1


def get_key():
    global key_last
    global cnt
    global flag
    a = key0.value()
    b = key1.value() << 1
    c = key2.value() << 2
    key = a | b | c
    key_out = 7
    if key_last != key and key_last == 7:
        time.sleep(10)
        a = key0.value()
        b = key1.value() << 1
        c = key2.value() << 2
        key = a | b | c
        key_out = key
        cnt = 0
        flag = 1
    elif key_last == key and key_last != 7 and (key == 4 or key == 5):
Example #8
0
 def init(self):
     #初始化
     MyMapperDict = {'Laser': Pin.cpu.C2}
     Pin.dict(MyMapperDict)  #映射引脚使用cpu的PC2
     self.pin = Pin('Laser', Pin.OUT_PP)
Example #9
0
 def init(self):
     #初始化
     MyMapperDict = {'Buzzer': Pin.cpu.E2}
     Pin.dict(MyMapperDict)  #映射引脚使用cpu的PE2
     self.pin = Pin('Buzzer', Pin.OUT_PP)
Example #10
0
 def __init__(self, pin_name):
     time.sleep(1)
     self.N1 = Pin(pin_name, Pin.OUT_PP)
     self.PinName = pin_name
     pyb.delay(10)
Example #11
0
# main.py -- put your code here!
from pyb import Pin
from stepermotor import SteperMotor

Pin_All = [Pin(p, Pin.OUT_PP) for p in ['X1', 'X2', 'X3', 'X4']]

if __name__ == '__main__':
    #转速(ms) 数值越大转速越慢 最小值1.8ms
    sm = SteperMotor(pin=Pin_All, speed=2)
    sm.steperRun(-360)
Example #12
0
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
'''

import pyb
import math
import gc
from pyb import LED, USB_VCP, DAC, Timer, ADC, Pin, UART, I2C
from array import array
from oled_938 import OLED_938
from mpu6050 import MPU6050
from drive import DRIVE

# Define various ports, pins and peripherals
a_out = DAC(1, bits=12)
a_in = ADC(Pin('X12'))
pot = ADC(Pin('X11'))
mic = ADC(Pin('Y11'))
r_LED = LED(1)
g_LED = LED(2)
y_LED = LED(3)
b_LED = LED(4)

g_pitch = 0

#  Configure X2:4, X7 as setting input pin - pull_up to receive switch settings
s0 = Pin('Y3', pyb.Pin.IN, pyb.Pin.PULL_UP)
s1 = Pin('X6', pyb.Pin.IN, pyb.Pin.PULL_UP)

# I2C connected to Y9, Y10 (I2C bus 2) and Y11 is reset low active
try:
Example #13
0
 def __init__(self, freq=100):
     self.pwm_pin = Pin('PA5', Pin.OUT)
     self.pwm_tim = Timer(2)  #not start
     self.flag = 0
     self.pp = 50
import sensor, time, pyb, math, lcd
#import m_lcd
from pyb import Pin, Timer, LED, UART
err_min = 120
err_max = 160
#黑色点阈值
black_threshold = [(0, 100)]
#xy平面误差数据
err_x = 0
err_y = 0

pin0 = Pin('P0', Pin.OUT_PP, Pin.PULL_NONE)

pin1 = Pin('P1', Pin.OUT_PP, Pin.PULL_NONE)

#发送数据
uart_buf = bytearray([0x55, 0xAA, 0x00, 0x00, 0x00, 0x00, 0xAA])
red_led = LED(1)
green_led = LED(2)
blue_led = LED(3)
ir_led = LED(4)
uart_data = 0


#读取串口缓存
def uart_read_buf():
    uart_data = 0
    if (uart.any()):  #判断是否有串口数据
        uart_data = uart.readchar()
    if uart_data == 0XAA:
        err_set = err_set + 1
from pyb import Pin, ADC, Timer
import array

f = 1000
N = 100

adc = ADC(Pin('A0'))  # Activation du CAN
buf = array.array("h", N * [0x7FFF])  # h = signed short (int 2 octets)
tim = Timer(1, freq=f)  # create a timer running at 10Hz

adc.read_timed(buf, tim)  # Mesures

# Données
x = [i * 1 / f * 1000 for i in range(N)]
y = [val for val in buf]

for i in range(len(x)):
    print(x[i], y[i])
Example #16
0
import time
from pyb import I2C
from pyb import Pin

p_out = Pin('X12', Pin.OUT_PP)  # Reset Pin


class ST7032I(object):
    def __init__(self):
        self.reset()
        self.init_lcd()

    def reset(self):
        p_out.low()
        time.sleep(0.1)
        p_out.high()
        time.sleep(0.1)

    def send_command(self, command):
        data = bytearray(2)
        data[0] = 0x00
        data[1] = command
        self.i2c.send(data, addr=0x3e)

    def send_character(self, character):
        data = bytearray(2)
        data[0] = 0x40
        data[1] = character
        self.i2c.send(data, addr=0x3e)

    def init_lcd(self):
Example #17
0
import sensor, image, time
from pyb import Pin
from pyb import LED

move_left     = Pin('P1', Pin.OUT_PP)
move_right    = Pin('P2', Pin.OUT_PP)
move_forword  = Pin('P3', Pin.OUT_PP)
move_back     = Pin('P4', Pin.OUT_PP)

# Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
# The below thresholds track in general red/green/blue things. You may wish to tune them...
thresholds = [(30, 100, 15, 127, 15, 127),  # generic_red_thresholds
              (30, 100, -64, -8, -32, 32),  # generic_green_thresholds
              (0, 50, 0, 64, -80, 0),       # generic_blue_thresholds
              (0,40,-15,40,-14,20)]         # black

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)  #160x120
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()
led2 = LED(2)#green,get x,y dispaly
led3 = LED(3)#run display

# 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. "merge=True" merges all overlapping blobs in the image.

while(True):
import pyb
from pyb import Pin, Timer, ADC, DAC, LED, UART
from array import array			# need this for memory allocation to buffers
from oled_938 import OLED_938	# Use OLED display driver
from drive import DRIVE


uart = UART(6)
uart.init(9600, bits=8, parity = None, stop = 2)
#motor = DRIVE()
speed = 60

A1 = Pin ('X3', Pin.OUT_PP)   # Control direction of motor A
A2 = Pin('X4', Pin.OUT_PP)
PWMA = Pin('X1')              # Control speed of motor A
B1 = Pin ('X7', Pin.OUT_PP)   # Control direction of motor B
B2 = Pin('X8', Pin.OUT_PP)
PWMB = Pin('X2')              # Control speed of motor B

# Configure timer 2 to produce 1KHz clock for PWM Controltim = Timer(2, freq = 1000)
tim = Timer(2, freq = 1000)
motorA = tim.channel(1, Timer.PWM, pin = PWMA)
motorB = tim.channel(2, Timer.PWM, pin = PWMB)

def A_forward(value):
    A1.low()
    A2.high()
    motorA.pulse_width_percent(value)

def A_back(value):
    A1.high()
Example #19
0
oled.display()
print('Performing Milestone 1')
print('Waiting for button press')

# Trigger setup
trigger = pyb.Switch()
while not trigger():
    time.sleep(0.001)
while trigger():
    pass
print('Button pressed - Running')
oled.draw_text(0, 30, 'Running Milestone 1')
oled.display()

# Setting up the motors
A1 = Pin('X3', Pin.OUT_PP)  # A is right motor
A2 = Pin('X4', Pin.OUT_PP)
B1 = Pin('X7', Pin.OUT_PP)  # B is left motor
B2 = Pin('X8', Pin.OUT_PP)
PWMA = Pin('X1')
PWMB = Pin('X2')

tim = Timer(2, freq=1000)
motorA = tim.channel(1, Timer.PWM, pin=PWMA)
motorB = tim.channel(2, Timer.PWM, pin=PWMB)

pot = pyb.ADC(Pin('X11'))

uart = UART(6)
uart.init(9600, bits=8, parity=None, stop=2)
Example #20
0
 def set_current_sense(self, cs, cs_dis):
     self.CS_DIS = Pin(cs_dis, Pin.OUT_PP)
     self.CS = Pin(cs, Pin.IN)
     self.current = ADC(self.CS)
Example #21
0
class Keypad():
    pin0 = Pin('P0', Pin.IN, Pin.PULL_UP)
    pin1 = Pin('P1', Pin.IN, Pin.PULL_UP)
    pin2 = Pin('P2', Pin.IN, Pin.PULL_UP)
    pin3 = Pin('P3', Pin.IN, Pin.PULL_UP)

    pin4 = Pin('P6', Pin.OUT_PP, Pin.PULL_NONE)
    pin5 = Pin('P7', Pin.OUT_PP, Pin.PULL_NONE)
    pin6 = Pin('P8', Pin.OUT_PP, Pin.PULL_NONE)
    pin7 = Pin('P9', Pin.OUT_PP, Pin.PULL_NONE)

    state = -1
    input_string = ""

    task_number_from_keypad = 0
    paramater_list = [254, 254, 254]

    def set_column1_to_0(self):
        self.pin4.value(0)

    def set_column1_to_1(self):
        self.pin4.value(1)

    def set_column2_to_0(self):
        self.pin5.value(0)

    def set_column2_to_1(self):
        self.pin5.value(1)

    def set_column3_to_0(self):
        self.pin6.value(0)

    def set_column3_to_1(self):
        self.pin6.value(1)

    def set_column4_to_0(self):
        self.pin7.value(0)

    def set_column4_to_1(self):
        self.pin7.value(1)

    def input_of_row1(self):
        return self.pin0.value()

    def input_of_row2(self):
        return self.pin1.value()

    def input_of_row3(self):
        return self.pin2.value()

    def input_of_row4(self):
        return self.pin3.value()

    def millisecond_of_delay(self, t):
        sleep_ms(t)

    def catch_keypad_input(self):
        if (not self.input_of_row1()):
            self.set_column1_to_1()
            if (self.input_of_row1()):
                self.millisecond_of_delay(500)
                if (self.input_of_row1()):
                    self.handle_keypad_number(15)
                    self.set_column1_to_0()
                    return 0  # must return, otherwise, a weird thing will happen
            self.set_column1_to_0()

            self.set_column2_to_1()
            if (self.input_of_row1()):
                self.millisecond_of_delay(500)
                if (self.input_of_row1()):
                    self.handle_keypad_number(0)
                    self.set_column2_to_0()
                    return 0  # must return, otherwise, a weird thing will happen
            self.set_column2_to_0()

            self.set_column3_to_1()
            if (self.input_of_row1()):
                self.millisecond_of_delay(500)
                if (self.input_of_row1()):
                    self.handle_keypad_number(10)
                    self.set_column3_to_0()
                    return 0  # must return, otherwise, a weird thing will happen
            self.set_column3_to_0()

            self.set_column4_to_1()
            if (self.input_of_row1()):
                self.millisecond_of_delay(500)
                if (self.input_of_row1()):
                    self.handle_keypad_number(14)
                    self.set_column4_to_0()
                    return 0  # must return, otherwise, a weird thing will happen
            self.set_column4_to_0()

        elif (not self.input_of_row2()):
            self.set_column1_to_1()
            if (self.input_of_row2()):
                self.millisecond_of_delay(500)
                if (self.input_of_row2()):
                    self.handle_keypad_number(7)
                    self.set_column1_to_0()
                    return 0  # must return, otherwise, a weird thing will happen
            self.set_column1_to_0()

            self.set_column2_to_1()
            if (self.input_of_row2()):
                self.millisecond_of_delay(500)
                if (self.input_of_row2()):
                    self.handle_keypad_number(8)
                    self.set_column2_to_0()
                    return 0  # must return, otherwise, a weird thing will happen
            self.set_column2_to_0()

            self.set_column3_to_1()
            if (self.input_of_row2()):
                self.millisecond_of_delay(500)
                if (self.input_of_row2()):
                    self.handle_keypad_number(9)
                    self.set_column3_to_0()
                    return 0  # must return, otherwise, a weird thing will happen
            self.set_column3_to_0()

            self.set_column4_to_1()
            if (self.input_of_row2()):
                self.millisecond_of_delay(500)
                if (self.input_of_row2()):
                    self.handle_keypad_number(13)
                    self.set_column4_to_0()
                    return 0  # must return, otherwise, a weird thing will happen
            self.set_column4_to_0()

        elif (not self.input_of_row3()):
            self.set_column1_to_1()
            if (self.input_of_row3()):
                self.millisecond_of_delay(500)
                if (self.input_of_row3()):
                    self.handle_keypad_number(4)
                    self.set_column1_to_0()
                    return 0  # must return, otherwise, a weird thing will happen
            self.set_column1_to_0()

            self.set_column2_to_1()
            if (self.input_of_row3()):
                self.millisecond_of_delay(500)
                if (self.input_of_row3()):
                    self.handle_keypad_number(5)
                    self.set_column2_to_0()
                    return 0  # must return, otherwise, a weird thing will happen
            self.set_column2_to_0()

            self.set_column3_to_1()
            if (self.input_of_row3()):
                self.millisecond_of_delay(500)
                if (self.input_of_row3()):
                    self.handle_keypad_number(6)
                    self.set_column3_to_0()
                    return 0  # must return, otherwise, a weird thing will happen
            self.set_column3_to_0()

            self.set_column4_to_1()
            if (self.input_of_row3()):
                self.millisecond_of_delay(500)
                if (self.input_of_row3()):
                    # self.handle_keypad_number(12)
                    self.set_column4_to_0()
                    return 0  # must return, otherwise, a weird thing will happen
            self.set_column4_to_0()

        elif (not self.input_of_row4()):
            self.set_column1_to_1()
            if (self.input_of_row4()):
                self.millisecond_of_delay(500)
                if (self.input_of_row4()):
                    self.handle_keypad_number(1)
                    self.set_column1_to_0()
                    return 0  # must return, otherwise, a weird thing will happen
            self.set_column1_to_0()

            self.set_column2_to_1()
            if (self.input_of_row4()):
                self.millisecond_of_delay(500)
                if (self.input_of_row4()):
                    self.handle_keypad_number(2)
                    self.set_column2_to_0()
                    return 0  # must return, otherwise, a weird thing will happen
            self.set_column2_to_0()

            self.set_column3_to_1()
            if (self.input_of_row4()):
                self.millisecond_of_delay(500)
                if (self.input_of_row4()):
                    self.handle_keypad_number(3)
                    self.set_column3_to_0()
                    return 0  # must return, otherwise, a weird thing will happen
            self.set_column3_to_0()

            self.set_column4_to_1()
            if (self.input_of_row4()):
                self.millisecond_of_delay(500)
                if (self.input_of_row4()):
                    # self.handle_keypad_number(11)
                    self.set_column4_to_0()
                    return 0  # must return, otherwise, a weird thing will happen
            self.set_column4_to_0()

    def handle_keypad_number(self, number):
        """
        1	2	3	Return(or back)
        4	5	6	Menu
        7	8	9	Cancel
            0	.	Enter

        : 15
        .: 10
        Return: 11
        Menu: 12
        Cancel: 13
        Enter: 14

        first press number + confire_key to SET task number
        then parameter
        """
        led.on()

        if (number != 10):  # != resend key
            if (number < 10):
                self.input_string += str(number)
            elif (number == 14 or number == 15):  # confire button
                try:
                    self.state += 1
                    if (self.state == 0):
                        self.task_number_from_keypad = int(self.input_string)
                        self.input_string = ""
                    if (self.state == 1):
                        self.paramater_list[0] = int(self.input_string)
                        self.input_string = ""
                    if (self.state == 2):
                        self.paramater_list[1] = int(self.input_string)
                        self.input_string = ""
                    if (self.state == 3):
                        self.paramater_list[2] = int(self.input_string)
                        self.input_string = ""
                    if (self.state > 3):
                        return
                except Exception as e:
                    self.state = -1
                    self.input_string = ""
                    self.paramater_list = [254, 254, 254]
                    self.task_number_from_keypad = 0
            elif (number == 11 or number == 12
                  or number == 13):  # cancle button
                self.state = -1
                self.input_string = ""
                self.paramater_list = [254, 254, 254]
                self.task_number_from_keypad = 0

        # send data according to different task
        if (self.task_number_from_keypad != 0):
            send_signal(self.task_number_from_keypad,
                        data=[
                            self.paramater_list[0], self.paramater_list[1],
                            self.paramater_list[2]
                        ])

        print("key:", number, "task:", self.task_number_from_keypad, "p:",
              self.paramater_list)

        sleep_ms(150)
        led.off()
Example #22
0
 def __init__(self, tim_num, channel, frequency, pin):
     self.tim = Timer(tim_num, freq=frequency)
     self.pin = Pin(pin)
     self.channel = channel
     self.scale = Timer.source_freq(self.tim)/(Timer.prescaler(self.tim)+1)
Example #23
0
# Defines the I2C bus that will be used
I2C_BUS1 = 1

############################################################
# Application Variables
############################################################

# List object that contains the duty cycle for RGB
# Valid values are 0 - 100. Due to the hardware, the
# duty cycle is reversed! 0% provides a ground which is
# full on to the LED's. 100% is full voltage and LED is off.
DutyCycle = 100

# Defines the pins used to drive the RGB duty cycle
PinList = [Pin('X1'), Pin('X2'), Pin('X3')]

# Defines the timers used to generate the PWM
TimerList = [2, 2, 2]

# Defines the timer frequency in Hz for the RGB
FrequencyList = [1000, 1000, 1000]

# Specifies the timer channels used to drive the RGB LED's
TimerChList = [1, 2, 3]

# Holds the button state based on how many times its been
# pressed
System_State = 0

# If 0, the duty cycle is counting down.
Example #24
0
 def __init__(self, tim_num, channel, frequency, pin):
     self.tim = Timer(tim_num, freq=frequency)
     self.pin = Pin(pin)
     self.channel = channel
Example #25
0
        # print("Updated GPS Object...")
        global new_data  # Use Global to trigger update
        new_data = True



# Setup the connection to your GPS here
uart = UART(6, 9600, read_buf_len=1000)

# Create an external interrupt on pin X8
pps_pin = pyb.Pin.board.X8
extint = pyb.ExtInt(pps_pin, pyb.ExtInt.IRQ_FALLING, pyb.Pin.PULL_UP,
                    pps_callback)

# Create relay object
relay = Pin('Y11', Pin.OUT_PP)

# Setup rtc timer to wake up pyboard every 30 seconds during standby to check
# conditions
# rtc = pyb.RTC()
# rtc.wakeup(30000)




# #######################  GPS functions  #####################################

EARTH_RADIUS = 6370000
MAG_LAT = 82.7
MAG_LON = -114.4
print('Initialising Autodrive Module')
print('Version 1.0')

from pyb import Pin, ADC, Timer

# Key variables --------------------------------------------------
speed = 50 # standard driving speed
critdistance = 40 # critical stopping distance in cm
distance = 100 # sets initial distance to stop preventCollision() from running


# Defining the motor modules--------------------------------------
A1 = Pin('Y9',Pin.OUT_PP) # motor A is on the RHS of the vehicle
A2 = Pin('Y10',Pin.OUT_PP)
motor1 = Pin('X1')

B1 = Pin('Y11',Pin.OUT_PP) # motor B is on the LHS of the vehicle
B2 = Pin('Y12',Pin.OUT_PP)
motor2 = Pin('X2')

tim = Timer(2, freq = 1000)
ch1 = tim.channel(1, Timer.PWM, pin = motor1)
ch2 = tim.channel(2, Timer.PWM, pin = motor2)

# Ultrasound Echo Initialising -----------------------------------
Trigger = Pin('X3', Pin.OUT_PP)
Echo = Pin('X4',Pin.IN)

# Create a microseconds counter.
micros = pyb.Timer(5, prescaler=83, period=0x3fffffff) #** Use timer 5 instead of 2
micros.counter(0)
# This example shows off how to use the NCC feature of your OpenMV Cam to match
# image patches to parts of an image... expect for extremely controlled enviorments
# NCC is not all to useful.
#
# WARNING: NCC supports needs to be reworked! As of right now this feature needs
# a lot of work to be made into somethin useful. This script will reamin to show
# that the functionality exists, but, in its current state is inadequate.

import sensor, image
import time, utime
from pyb import UART
from image import SEARCH_EX, SEARCH_DS
from pyb import Pin, LED

led = LED(3)
pin7 = Pin('P7', Pin.IN, Pin.PULL_UP)

# Reset sensor
sensor.reset()

# Set sensor settings
sensor.set_contrast(1)
sensor.set_gainceiling(16)
# Max resolution for template matching with SEARCH_EX is QQVGA
sensor.set_framesize(sensor.QQVGA)
# You can set windowing to reduce the search image.
#sensor.set_windowing(((640-80)//2, (480-60)//2, 80, 60))
#sensor.set_windowing((60,60))
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.skip_frames(time=2000)
Example #28
0
from time import sleep
from pyb import Pin, SPI

rst = Pin('PD12', Pin.OUT_PP, Pin.PULL_UP)
rs  = Pin('PD13', Pin.OUT_PP, Pin.PULL_UP)
cs  = Pin('PB12', Pin.OUT_PP, Pin.PULL_UP)
bl  = Pin('PA5',  Pin.OUT_PP, Pin.PULL_UP)
spi = SPI(2, SPI.MASTER, baudrate=22500000, polarity=0, phase=0) 

def reset():
    rst.low()
    sleep(100)
    rst.high()
    sleep(100)

def write_command(c):
    cs.low()
    rs.low()
    spi.send(c)
    cs.high()

def write_data(c):
    cs.low()
    rs.high()
    spi.send(c)
    cs.high()

def clear(c=0x00):
    write_command(0x2C)
    for i in range(128*160):
        write_data(c)
Example #29
0
uart_buf = bytearray([0x55,0xAA,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xAA])
#超声波回调处理函数
def Ultrasound(line):
   if(Echo.value()==1):
        tim_count.init(prescaler=1799, period=2500)#打开定时器
   if(Echo.value()==0):
        global highcnt
        highcnt = tim_count.counter()#计数
        tim_count.deinit()
#发送函数
def tick(timer):
    uart.write(uart_buf)
#超声波发射端口配置
timpwm = Timer(4, freq=30) #超声波60赫兹发射频率
ch1 = timpwm.channel(1, Timer.PWM, pin=Pin("P7"), pulse_width=80) #100us发射角
#超声波接收端口配置
tim_count = pyb.Timer(1) #定时器计数
extint = pyb.ExtInt('P0', pyb.ExtInt.IRQ_RISING_FALLING, pyb.Pin.PULL_DOWN,Ultrasound)#开启外部中断
Echo = Pin('P0', Pin.IN, Pin.PULL_DOWN)
#timer发送配置
senddata = Timer(2, freq=60)
senddata.callback(tick)
#串口三配置
uart = UART(3, 115200)
uart.init(115200, bits=8, parity=None, stop=1)

sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)#设置灰度信息
sensor.set_framesize(sensor.QQVGA2)#设置图像大小
sensor.skip_frames(20)#相机自检几张图片
from pyb import Pin
from onewire import OneWire
from ds18x20 import DS18X20
from time import sleep_ms

pinD1 = Pin("D5")
ds = DS18X20(OneWire(pinD1))

roms = ds.scan()
print('found probes:', roms)

ds.convert_temp()
temp = ds.read_temp(roms[0])
print(temp)