mirror of
https://github.com/openmv/openmv.git
synced 2025-11-04 14:49:50 +08:00
Merge pull request #2149 from kwagyeman/kwabena/add_machine_board_control_examples
scripts/examples: Add imxrt board control examples.
This commit is contained in:
commit
820e59df58
@ -0,0 +1,17 @@
|
||||
# This work is licensed under the MIT license.
|
||||
# Copyright (c) 2013-2024 OpenMV LLC. All rights reserved.
|
||||
# https://github.com/openmv/openmv/blob/master/LICENSE
|
||||
#
|
||||
# ADC Read Example.
|
||||
#
|
||||
# This example shows how to use the ADC to read an analog pin.
|
||||
|
||||
import time
|
||||
from machine import ADC
|
||||
|
||||
adc = ADC("P6") # Must always be "P6".
|
||||
|
||||
while True:
|
||||
# The ADC has 16-bits of resolution for 65536 values.
|
||||
print("ADC = %fv" % ((adc.read_u16() * 3.3) / 65535))
|
||||
time.sleep_ms(100)
|
||||
@ -0,0 +1,32 @@
|
||||
# This work is licensed under the MIT license.
|
||||
# Copyright (c) 2013-2024 OpenMV LLC. All rights reserved.
|
||||
# https://github.com/openmv/openmv/blob/master/LICENSE
|
||||
#
|
||||
# CAN Shield Example
|
||||
#
|
||||
# This example demonstrates CAN communications between two cameras.
|
||||
# NOTE: you need two CAN transceiver shields and DB9 cable to run this example.
|
||||
|
||||
import time
|
||||
from machine import CAN
|
||||
|
||||
# NOTE: Set to False on receiving node.
|
||||
TRANSMITTER = True
|
||||
|
||||
can = CAN(0, CAN.NORMAL, baudrate=1000000, auto_restart=True)
|
||||
|
||||
if TRANSMITTER:
|
||||
while True:
|
||||
# Send message with id 1
|
||||
can.send("Hello", 1, timeout=100, extframe=False)
|
||||
time.sleep_ms(1000)
|
||||
|
||||
else:
|
||||
# Runs on the receiving node.
|
||||
# Set a filter to receive messages with id=1 and 2
|
||||
# Filter index, mode (DUAL, etc..), FIFO (0), params
|
||||
can.setfilter(0, CAN.DUAL, 0, [1, 2])
|
||||
|
||||
while True:
|
||||
# Receive messages on FIFO 0 (there's only one fifo)
|
||||
print(can.recv(0, timeout=10000))
|
||||
@ -0,0 +1,25 @@
|
||||
# This work is licensed under the MIT license.
|
||||
# Copyright (c) 2013-2024 OpenMV LLC. All rights reserved.
|
||||
# https://github.com/openmv/openmv/blob/master/LICENSE
|
||||
#
|
||||
# I2C Control
|
||||
#
|
||||
# This example shows how to use the i2c bus on your OpenMV Cam by dumping the
|
||||
# contents on a standard EEPROM. To run this example either connect the
|
||||
# Thermopile Shield to your OpenMV Cam or an I2C EEPROM to your OpenMV Cam.
|
||||
|
||||
from machine import I2C
|
||||
|
||||
i2c = I2C(1) # The i2c bus must always be 1.
|
||||
print(i2c.scan()) # Show attached devices.
|
||||
mem = i2c.readfrom_mem(0x50, 0, 256) # The eeprom slave address is 0x50.
|
||||
|
||||
print("\n[")
|
||||
for i in range(16):
|
||||
print("\t[", end="")
|
||||
for j in range(16):
|
||||
print("%03d" % mem[(i * 16) + j], end="")
|
||||
if j != 15:
|
||||
print(", ", end="")
|
||||
print("]," if i != 15 else "]")
|
||||
print("]")
|
||||
@ -0,0 +1,35 @@
|
||||
# This work is licensed under the MIT license.
|
||||
# Copyright (c) 2013-2024 OpenMV LLC. All rights reserved.
|
||||
# https://github.com/openmv/openmv/blob/master/LICENSE
|
||||
#
|
||||
# LED Control Example
|
||||
#
|
||||
# This example shows how to control your OpenMV Cam's built-in LEDs.
|
||||
|
||||
import time
|
||||
from machine import LED
|
||||
|
||||
red_led = LED("LED_RED")
|
||||
green_led = LED("LED_GREEN")
|
||||
blue_led = LED("LED_BLUE")
|
||||
|
||||
|
||||
def led_control(x):
|
||||
if (x & 1) == 0:
|
||||
red_led.off()
|
||||
elif (x & 1) == 1:
|
||||
red_led.on()
|
||||
if (x & 2) == 0:
|
||||
green_led.off()
|
||||
elif (x & 2) == 2:
|
||||
green_led.on()
|
||||
if (x & 4) == 0:
|
||||
blue_led.off()
|
||||
elif (x & 4) == 4:
|
||||
blue_led.on()
|
||||
|
||||
|
||||
while True:
|
||||
for i in range(8):
|
||||
led_control(i)
|
||||
time.sleep_ms(500)
|
||||
@ -0,0 +1,31 @@
|
||||
# This work is licensed under the MIT license.
|
||||
# Copyright (c) 2013-2024 OpenMV LLC. All rights reserved.
|
||||
# https://github.com/openmv/openmv/blob/master/LICENSE
|
||||
#
|
||||
# LED Control Example
|
||||
#
|
||||
# This example shows how to control your OpenMV Cam's built-in LEDs.
|
||||
|
||||
import time
|
||||
from machine import PWM
|
||||
|
||||
r = PWM("LED_RED", freq=200, duty_u16=65535)
|
||||
b = PWM("LED_BLUE", freq=200, duty_u16=65535)
|
||||
g = PWM("LED_GREEN", freq=200, duty_u16=65535)
|
||||
|
||||
|
||||
while True:
|
||||
for i in range(0, 65536, 256):
|
||||
r.duty_u16(65535 - i)
|
||||
time.sleep_ms(10)
|
||||
r.duty_u16(65535)
|
||||
|
||||
for i in range(0, 65536, 256):
|
||||
g.duty_u16(65535 - i)
|
||||
time.sleep_ms(10)
|
||||
g.duty_u16(65535)
|
||||
|
||||
for i in range(0, 65536, 256):
|
||||
b.duty_u16(65535 - i)
|
||||
time.sleep_ms(10)
|
||||
b.duty_u16(65535)
|
||||
@ -0,0 +1,61 @@
|
||||
# This work is licensed under the MIT license.
|
||||
# Copyright (c) 2013-2024 OpenMV LLC. All rights reserved.
|
||||
# https://github.com/openmv/openmv/blob/master/LICENSE
|
||||
#
|
||||
# Motor Control Example
|
||||
#
|
||||
# This example shows how to control a DIR and PWM motor controller
|
||||
# with your OpenMV Cam. "P0"/"P2" are DIR pins for left/right motors
|
||||
# and "P7"/"P8" are the PWM (speed) for the left/right motors.
|
||||
|
||||
import time
|
||||
from machine import Pin, PWM
|
||||
|
||||
p0 = Pin("P0", Pin.OUT)
|
||||
p2 = Pin("P2", Pin.OUT)
|
||||
p7 = PWM("P7", freq=10000, duty_u16=0)
|
||||
p8 = PWM("P8", freq=10000, duty_u16=0)
|
||||
|
||||
p0.value(0)
|
||||
p2.value(0)
|
||||
|
||||
while True:
|
||||
for i in range(0, 65535, 100):
|
||||
p7.duty_u16(i)
|
||||
time.sleep_ms(10)
|
||||
|
||||
for i in range(65535, 0, -100):
|
||||
p7.duty_u16(i)
|
||||
time.sleep_ms(10)
|
||||
|
||||
p0.value(not p0.value())
|
||||
|
||||
for i in range(0, 65535, 100):
|
||||
p7.duty_u16(i)
|
||||
time.sleep_ms(10)
|
||||
|
||||
for i in range(65535, 0, -100):
|
||||
p7.duty_u16(i)
|
||||
time.sleep_ms(10)
|
||||
|
||||
p0.value(not p0.value())
|
||||
|
||||
for i in range(0, 65535, 100):
|
||||
p8.duty_u16(i)
|
||||
time.sleep_ms(10)
|
||||
|
||||
for i in range(65535, 0, -100):
|
||||
p8.duty_u16(i)
|
||||
time.sleep_ms(10)
|
||||
|
||||
p2.value(not p2.value())
|
||||
|
||||
for i in range(0, 65535, 100):
|
||||
p8.duty_u16(i)
|
||||
time.sleep_ms(10)
|
||||
|
||||
for i in range(65535, 0, -100):
|
||||
p8.duty_u16(i)
|
||||
time.sleep_ms(10)
|
||||
|
||||
p2.value(not p2.value())
|
||||
@ -0,0 +1,41 @@
|
||||
# This work is licensed under the MIT license.
|
||||
# Copyright (c) 2013-2024 OpenMV LLC. All rights reserved.
|
||||
# https://github.com/openmv/openmv/blob/master/LICENSE
|
||||
#
|
||||
# Motor Control Example
|
||||
#
|
||||
# This example shows off how to control an hbridge motor
|
||||
# controller with "P0"/"P7" controlling a left-motor hbridge
|
||||
# and "P2"/"P8" controlling a right-motor hbridge.
|
||||
|
||||
import time
|
||||
from machine import Pin
|
||||
|
||||
p0 = Pin("P0", Pin.OUT)
|
||||
p2 = Pin("P2", Pin.OUT)
|
||||
p7 = Pin("P7", Pin.OUT)
|
||||
p8 = Pin("P8", Pin.OUT)
|
||||
|
||||
p0.value(0)
|
||||
p2.value(0)
|
||||
p7.value(0)
|
||||
p8.value(0)
|
||||
|
||||
while True:
|
||||
p0.value(not p0.value())
|
||||
time.sleep_ms(2000)
|
||||
p7.value(not p7.value())
|
||||
time.sleep_ms(2000)
|
||||
p0.value(not p0.value())
|
||||
time.sleep_ms(2000)
|
||||
p7.value(not p7.value())
|
||||
time.sleep_ms(2000)
|
||||
|
||||
p2.value(not p2.value())
|
||||
time.sleep_ms(2000)
|
||||
p8.value(not p8.value())
|
||||
time.sleep_ms(2000)
|
||||
p2.value(not p2.value())
|
||||
time.sleep_ms(2000)
|
||||
p8.value(not p8.value())
|
||||
time.sleep_ms(2000)
|
||||
@ -0,0 +1,17 @@
|
||||
# This work is licensed under the MIT license.
|
||||
# Copyright (c) 2013-2024 OpenMV LLC. All rights reserved.
|
||||
# https://github.com/openmv/openmv/blob/master/LICENSE
|
||||
#
|
||||
# Pin Control Example
|
||||
#
|
||||
# This example shows how to use the I/O pins in GPIO mode on your OpenMV Cam.
|
||||
|
||||
from machine import Pin
|
||||
|
||||
# Connect a switch to pin 0 that will pull it low when the switch is closed.
|
||||
# Pin 1 will then light up.
|
||||
pin0 = Pin("P0", Pin.IN, Pin.PULL_UP)
|
||||
pin1 = Pin("P1", Pin.OUT)
|
||||
|
||||
while True:
|
||||
pin1.value(not pin0.value())
|
||||
@ -0,0 +1,22 @@
|
||||
# This work is licensed under the MIT license.
|
||||
# Copyright (c) 2013-2024 OpenMV LLC. All rights reserved.
|
||||
# https://github.com/openmv/openmv/blob/master/LICENSE
|
||||
#
|
||||
# Power Good Pin
|
||||
#
|
||||
# The power good pin is an internal Pin on your OpenMV Cam
|
||||
# which goes low when the board external power and high when
|
||||
# it does not. You can use this pin to tell if you are running
|
||||
# an external battery.
|
||||
#
|
||||
# Run this example with a battery attached and then disconnect
|
||||
# your OpenMV Cam from the computer to see the red light turn on.
|
||||
# When you reconnect the camera to power it will turn off.
|
||||
|
||||
import machine
|
||||
|
||||
pg = machine.Pin("PG", machine.Pin.IN)
|
||||
r = machine.LED("LED_RED")
|
||||
|
||||
while True:
|
||||
r.value(pg.value())
|
||||
@ -0,0 +1,45 @@
|
||||
# This work is licensed under the MIT license.
|
||||
# Copyright (c) 2013-2024 OpenMV LLC. All rights reserved.
|
||||
# https://github.com/openmv/openmv/blob/master/LICENSE
|
||||
#
|
||||
# This work is licensed under the MIT license.
|
||||
# Copyright (c) 2013-2024 OpenMV LLC. All rights reserved.
|
||||
# https://github.com/openmv/openmv/blob/master/LICENSE
|
||||
#
|
||||
# PWM Control Example
|
||||
#
|
||||
# This example shows how to do PWM with your OpenMV Cam.
|
||||
|
||||
import time
|
||||
from machine import PWM
|
||||
|
||||
# P7 and P8 may share the same PWM module they need
|
||||
# to have the same frequency.
|
||||
p7 = PWM("P7", freq=100, duty_u16=32768)
|
||||
p8 = PWM("P8", freq=100, duty_u16=32768)
|
||||
|
||||
# P9 and P10 may share the same PWM module they need
|
||||
# to have the same frequency.
|
||||
p9 = PWM("P9", freq=100, duty_u16=32768)
|
||||
p10 = PWM("P10", freq=100, duty_u16=32768)
|
||||
|
||||
while True:
|
||||
for i in range(0, 65536, 256):
|
||||
p7.duty_u16(65535 - i)
|
||||
time.sleep_ms(10)
|
||||
p7.duty_u16(32768)
|
||||
|
||||
for i in range(0, 65536, 256):
|
||||
p8.duty_u16(65535 - i)
|
||||
time.sleep_ms(10)
|
||||
p8.duty_u16(32768)
|
||||
|
||||
for i in range(0, 65536, 256):
|
||||
p9.duty_u16(65535 - i)
|
||||
time.sleep_ms(10)
|
||||
p9.duty_u16(32768)
|
||||
|
||||
for i in range(0, 65536, 256):
|
||||
p10.duty_u16(65535 - i)
|
||||
time.sleep_ms(10)
|
||||
p10.duty_u16(32768)
|
||||
@ -0,0 +1,21 @@
|
||||
# This work is licensed under the MIT license.
|
||||
# Copyright (c) 2013-2024 OpenMV LLC. All rights reserved.
|
||||
# https://github.com/openmv/openmv/blob/master/LICENSE
|
||||
#
|
||||
# RTC Example
|
||||
#
|
||||
# This example shows how to use the RTC.
|
||||
|
||||
import time
|
||||
from machine import RTC
|
||||
|
||||
rtc = RTC()
|
||||
|
||||
# Comment this out to initialize the RTC time and date.
|
||||
# After doing this the RTC will keep the time until
|
||||
# power is completely lost by the system.
|
||||
# rtc.init((2023, 8, 8, 14, 15, 0, 0, 0))
|
||||
|
||||
while True:
|
||||
time.sleep_ms(100)
|
||||
print(rtc.now())
|
||||
@ -0,0 +1,38 @@
|
||||
# This work is licensed under the MIT license.
|
||||
# Copyright (c) 2013-2024 OpenMV LLC. All rights reserved.
|
||||
# https://github.com/openmv/openmv/blob/master/LICENSE
|
||||
#
|
||||
# Servo Control Example
|
||||
#
|
||||
# This example shows how to use your OpenMV Cam to control servos.
|
||||
#
|
||||
# Servos need 50 Hz PWM with a 1000us to 2000us pulse width.
|
||||
|
||||
import time
|
||||
from machine import PWM
|
||||
|
||||
# P7 and P8 may share the same PWM module they need
|
||||
# to have the same frequency.
|
||||
p7 = PWM("P7", freq=50, duty_ns=(2000 * 1000))
|
||||
p8 = PWM("P8", freq=50, duty_ns=(2000 * 1000))
|
||||
|
||||
# P9 and P10 may share the same PWM module they need
|
||||
# to have the same frequency.
|
||||
p9 = PWM("P9", freq=50, duty_ns=(2000 * 1000))
|
||||
p10 = PWM("P10", freq=50, duty_ns=(2000 * 1000))
|
||||
|
||||
|
||||
while True:
|
||||
for i in range(1000, 2000, 100):
|
||||
p7.duty_ns(i * 1000)
|
||||
p8.duty_ns(i * 1000)
|
||||
p9.duty_ns(i * 1000)
|
||||
p10.duty_ns(i * 1000)
|
||||
time.sleep_ms(1000)
|
||||
|
||||
for i in range(2000, 1000, -100):
|
||||
p7.duty_ns(i * 1000)
|
||||
p8.duty_ns(i * 1000)
|
||||
p9.duty_ns(i * 1000)
|
||||
p10.duty_ns(i * 1000)
|
||||
time.sleep_ms(1000)
|
||||
@ -0,0 +1,88 @@
|
||||
# This work is licensed under the MIT license.
|
||||
# Copyright (c) 2013-2024 OpenMV LLC. All rights reserved.
|
||||
# https://github.com/openmv/openmv/blob/master/LICENSE
|
||||
#
|
||||
# SPI Control
|
||||
#
|
||||
# This example shows how to use the SPI bus to directly control the LCD shield without
|
||||
# using the built-in LCD driver. You will need the LCD shield to run this example.
|
||||
|
||||
import sensor
|
||||
import time
|
||||
from machine import Pin, SPI
|
||||
import struct
|
||||
|
||||
cs = Pin("P3", Pin.OUT)
|
||||
rst = Pin("P7", Pin.OUT)
|
||||
rs = Pin("P8", Pin.OUT)
|
||||
# The hardware SPI bus for your OpenMV Cam is always SPI bus 1.
|
||||
|
||||
# NOTE: The SPI clock frequency will not always be the requested frequency. The hardware only supports
|
||||
# frequencies that are the bus frequency divided by a prescaler (which can be 2, 4, 8, 16, 32, 64, 128 or 256).
|
||||
spi = SPI(1, baudrate=int(1000000000 / 66), polarity=0, phase=0)
|
||||
|
||||
|
||||
def write_command_byte(c):
|
||||
cs.low()
|
||||
rs.low()
|
||||
spi.write(bytes([c]))
|
||||
cs.high()
|
||||
|
||||
|
||||
def write_data_byte(c):
|
||||
cs.low()
|
||||
rs.high()
|
||||
spi.write(bytes([c]))
|
||||
cs.high()
|
||||
|
||||
|
||||
def write_command(c, *data):
|
||||
write_command_byte(c)
|
||||
if data:
|
||||
for d in data:
|
||||
write_data_byte(d)
|
||||
|
||||
|
||||
def write_image(img):
|
||||
cs.low()
|
||||
rs.high()
|
||||
reversed_img = struct.unpack('H' * (img.size() // 2), img)
|
||||
reversed_array = struct.pack('>' + 'H' * len(reversed_img), *reversed_img)
|
||||
spi.write(reversed_array)
|
||||
cs.high()
|
||||
|
||||
|
||||
# Reset the LCD.
|
||||
rst.low()
|
||||
time.sleep_ms(100)
|
||||
rst.high()
|
||||
time.sleep_ms(100)
|
||||
|
||||
write_command(0x11) # Sleep Exit
|
||||
time.sleep_ms(120)
|
||||
|
||||
# Memory Data Access Control
|
||||
# Write 0xC8 for BGR mode.
|
||||
write_command(0x36, 0xC0)
|
||||
|
||||
# Interface Pixel Format
|
||||
write_command(0x3A, 0x05)
|
||||
|
||||
sensor.reset() # Initialize the camera sensor.
|
||||
sensor.set_pixformat(sensor.RGB565) # must be this
|
||||
sensor.set_framesize(sensor.QQVGA2) # must be this
|
||||
sensor.skip_frames(time=2000) # Let new settings take affect.
|
||||
clock = time.clock() # Tracks FPS.
|
||||
|
||||
while True:
|
||||
clock.tick() # Track elapsed milliseconds between snapshots().
|
||||
img = sensor.snapshot() # Take a picture and return the image.
|
||||
|
||||
write_command(0x2C) # Write image command...
|
||||
write_image(img)
|
||||
|
||||
# Display On
|
||||
write_command(0x29)
|
||||
|
||||
print(clock.fps()) # Note: Your OpenMV Cam runs about half as fast while
|
||||
# connected to your computer. The FPS should increase once disconnected.
|
||||
@ -0,0 +1,18 @@
|
||||
# This work is licensed under the MIT license.
|
||||
# Copyright (c) 2013-2024 OpenMV LLC. All rights reserved.
|
||||
# https://github.com/openmv/openmv/blob/master/LICENSE
|
||||
#
|
||||
# Switch Pin
|
||||
#
|
||||
# The user switch on your OpenMV Cam is readable via the
|
||||
# "SW" Pin. The pin is debounced in hardware via an RC circuit.
|
||||
# So, you just need to read the pin state to get if the
|
||||
# switch is pressed or not.
|
||||
|
||||
import machine
|
||||
|
||||
sw = machine.Pin("SW", machine.Pin.IN)
|
||||
r = machine.LED("LED_RED")
|
||||
|
||||
while True:
|
||||
r.value(not sw.value())
|
||||
@ -0,0 +1,27 @@
|
||||
# This work is licensed under the MIT license.
|
||||
# Copyright (c) 2013-2024 OpenMV LLC. All rights reserved.
|
||||
# https://github.com/openmv/openmv/blob/master/LICENSE
|
||||
#
|
||||
# Timer Control Example
|
||||
#
|
||||
# This example shows how to use a timer for callbacks.
|
||||
|
||||
import time
|
||||
from machine import LED
|
||||
from machine import Timer
|
||||
|
||||
blue_led = LED("LED_BLUE")
|
||||
|
||||
|
||||
# we will receive the timer object when being called
|
||||
# Note: functions that allocate memory are Not allowed in callbacks
|
||||
def tick(timer):
|
||||
blue_led.toggle()
|
||||
|
||||
|
||||
# The machine module currently only supports virtual timers via -1.
|
||||
tim = Timer(-1, freq=1, callback=tick) # create a timer object - trigger at 1Hz
|
||||
print(tim)
|
||||
|
||||
while True:
|
||||
time.sleep_ms(1000)
|
||||
@ -0,0 +1,20 @@
|
||||
# This work is licensed under the MIT license.
|
||||
# Copyright (c) 2013-2024 OpenMV LLC. All rights reserved.
|
||||
# https://github.com/openmv/openmv/blob/master/LICENSE
|
||||
#
|
||||
# UART Control
|
||||
#
|
||||
# This example shows how to use the serial port on your OpenMV Cam. Attach pin
|
||||
# P4 to the serial input of a serial LCD screen to see "Hello World!" printed
|
||||
# on the serial LCD display.
|
||||
|
||||
import time
|
||||
from machine import UART
|
||||
|
||||
# Always pass UART 1 for the UART number for your OpenMV Cam.
|
||||
# The second argument is the UART baud rate.
|
||||
uart = UART(1, 19200, timeout_char=200)
|
||||
|
||||
while True:
|
||||
uart.write("Hello World!\r")
|
||||
time.sleep_ms(1000)
|
||||
@ -0,0 +1,30 @@
|
||||
# This work is licensed under the MIT license.
|
||||
# Copyright (c) 2013-2024 OpenMV LLC. All rights reserved.
|
||||
# https://github.com/openmv/openmv/blob/master/LICENSE
|
||||
#
|
||||
# Watch Dog Example
|
||||
#
|
||||
# This example shows how to use the Watch Dog Timer.
|
||||
#
|
||||
# When you press the SW button software will stop feeding
|
||||
# the watchdog timer causing a system reset.
|
||||
|
||||
import time
|
||||
from machine import WDT, Pin, LED
|
||||
|
||||
sw = Pin("SW", Pin.IN)
|
||||
b = LED("LED_BLUE")
|
||||
wdt = WDT(timeout=2000)
|
||||
|
||||
while True:
|
||||
b.on()
|
||||
time.sleep_ms(150)
|
||||
b.off()
|
||||
time.sleep_ms(100)
|
||||
b.on()
|
||||
time.sleep_ms(150)
|
||||
b.off()
|
||||
time.sleep_ms(600)
|
||||
|
||||
if sw.value():
|
||||
wdt.feed()
|
||||
Loading…
Reference in New Issue
Block a user