mirror of
https://github.com/openmv/openmv.git
synced 2025-11-04 14:49:50 +08:00
Update examples.
* Add Arduino UART example. * Update Arduino SPI example to use callbacks. * Remove printf from timer_control.py callback. * Add PWM channel 3 and servo 3 to pwm and servo examples.
This commit is contained in:
parent
cba3d7ee70
commit
f04b18fe6f
@ -8,7 +8,7 @@
|
|||||||
# OpenMV Cam Slave Select (P3) - Arduino Uno SS (10)
|
# OpenMV Cam Slave Select (P3) - Arduino Uno SS (10)
|
||||||
# OpenMV Cam Ground - Arduino Ground
|
# OpenMV Cam Ground - Arduino Ground
|
||||||
|
|
||||||
import pyb, ustruct
|
import pyb, ustruct, time
|
||||||
|
|
||||||
text = "Hello World!\n"
|
text = "Hello World!\n"
|
||||||
data = ustruct.pack("<bi%ds" % len(text), 85, len(text), text) # 85 is a sync char.
|
data = ustruct.pack("<bi%ds" % len(text), 85, len(text), text) # 85 is a sync char.
|
||||||
@ -19,9 +19,6 @@ data = ustruct.pack("<bi%ds" % len(text), 85, len(text), text) # 85 is a sync ch
|
|||||||
# "%ds" puts a string in the data stream. E.g. "13s" for "Hello World!\n" (13 chars).
|
# "%ds" puts a string in the data stream. E.g. "13s" for "Hello World!\n" (13 chars).
|
||||||
# See https://docs.python.org/3/library/struct.html
|
# See https://docs.python.org/3/library/struct.html
|
||||||
|
|
||||||
# Zero pad data to a multiple of 4 bytes plus 4 bytes.
|
|
||||||
data += "\x00" * (4 + (len(data) % 4))
|
|
||||||
|
|
||||||
# READ ME!!!
|
# READ ME!!!
|
||||||
#
|
#
|
||||||
# Please understand that when your OpenMV Cam is not the SPI master it may miss responding to
|
# Please understand that when your OpenMV Cam is not the SPI master it may miss responding to
|
||||||
@ -40,24 +37,22 @@ data += "\x00" * (4 + (len(data) % 4))
|
|||||||
# polarity = 0 -> clock is idle low.
|
# polarity = 0 -> clock is idle low.
|
||||||
# phase = 0 -> sample data on rising clock edge, output data on falling clock edge.
|
# phase = 0 -> sample data on rising clock edge, output data on falling clock edge.
|
||||||
spi = pyb.SPI(2, pyb.SPI.SLAVE, polarity=0, phase=0)
|
spi = pyb.SPI(2, pyb.SPI.SLAVE, polarity=0, phase=0)
|
||||||
pin = pyb.Pin("P3", pyb.Pin.IN, pull=pyb.Pin.PULL_UP)
|
|
||||||
print("Waiting for Arduino...")
|
|
||||||
|
|
||||||
# Note that for sync up to work correctly the OpenMV Cam must be running this script before the
|
# NSS callback.
|
||||||
# Arduino starts to poll the OpenMV Cam for data. Otherwise the SPI byte framing gets messed up,
|
def nss_callback(line):
|
||||||
# and etc. So, keep the Arduino in reset until the OpenMV Cam is "Waiting for Arduino...".
|
global spi, data
|
||||||
|
|
||||||
while(True):
|
|
||||||
while(pin.value()): pass
|
|
||||||
try:
|
try:
|
||||||
spi.send(data, timeout=1000)
|
spi.send(data, timeout=1000)
|
||||||
# If we failed to sync up the first time we'll sync up the next time.
|
|
||||||
print("Sent Data!") # Only reached on no error.
|
|
||||||
except OSError as err:
|
except OSError as err:
|
||||||
pass # Don't care about errors - so pass.
|
pass # Don't care about errors - so pass.
|
||||||
# Note that there are 3 possible errors. A timeout error, a general purpose error, or
|
# Note that there are 3 possible errors. A timeout error, a general purpose error, or
|
||||||
# a busy error. The error codes are 116, 5, 16 respectively for "err.arg[0]".
|
# a busy error. The error codes are 116, 5, 16 respectively for "err.arg[0]".
|
||||||
while(not pin.value()): pass
|
|
||||||
|
# Configure NSS/CS in IRQ mode to send data when requested by the master.
|
||||||
|
pyb.ExtInt(pyb.Pin("P3"), pyb.ExtInt.IRQ_FALLING, pyb.Pin.PULL_UP, nss_callback)
|
||||||
|
|
||||||
|
while(True):
|
||||||
|
time.sleep(1000)
|
||||||
|
|
||||||
###################################################################################################
|
###################################################################################################
|
||||||
# Arduino Code
|
# Arduino Code
|
||||||
@ -79,20 +74,18 @@ while(True):
|
|||||||
# }
|
# }
|
||||||
#
|
#
|
||||||
# void loop() {
|
# void loop() {
|
||||||
# int32_t temp = 0;
|
# int32_t len = 0;
|
||||||
# char buff[CHAR_BUF] = {0};
|
# char buff[CHAR_BUF] = {0};
|
||||||
# digitalWrite(SS_PIN, LOW);
|
# digitalWrite(SS_PIN, LOW);
|
||||||
# delay(1); // Give the OpenMV Cam some time to setup to send data.
|
# delay(1); // Give the OpenMV Cam some time to setup to send data.
|
||||||
#
|
#
|
||||||
# if(SPI.transfer(1) == 85) { // saw sync char?
|
# if(SPI.transfer(1) == 85) { // saw sync char?
|
||||||
# SPI.transfer(&temp, 4); // get length
|
# SPI.transfer(&len, 4); // get length
|
||||||
# int zero_legnth = 4 + ((temp + 1) % 4);
|
# if (len) {
|
||||||
# if (temp) {
|
# SPI.transfer(&buff, min(len, CHAR_BUF));
|
||||||
# SPI.transfer(&buff, min(temp, CHAR_BUF));
|
# len -= min(len, CHAR_BUF);
|
||||||
# temp -= min(temp, CHAR_BUF);
|
|
||||||
# }
|
# }
|
||||||
# while (temp--) SPI.transfer(0); // eat any remaining bytes
|
# while (len--) SPI.transfer(0); // eat any remaining bytes
|
||||||
# while (zero_legnth--) SPI.transfer(0); // eat zeros.
|
|
||||||
# }
|
# }
|
||||||
#
|
#
|
||||||
# digitalWrite(SS_PIN, HIGH);
|
# digitalWrite(SS_PIN, HIGH);
|
||||||
|
|||||||
38
scripts/examples/02-Board-Control/arduino_uart.py
Normal file
38
scripts/examples/02-Board-Control/arduino_uart.py
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
# Basic UART communications between OpenMV and Arduino Uno.
|
||||||
|
|
||||||
|
# 1) Wire up your OpenMV Cam to your Arduino Uno like this:
|
||||||
|
#
|
||||||
|
# OpenMV Cam Ground Pin ----> Arduino Ground
|
||||||
|
# OpenMV Cam UART3_TX(P4) ----> Arduino Uno UART_RX(0)
|
||||||
|
# OpenMV Cam UART3_RX(P5) ----> Arduino Uno UART_TX(1)
|
||||||
|
|
||||||
|
# 2) Uncomment and upload the following sketch to Arduino:
|
||||||
|
#
|
||||||
|
# void setup() {
|
||||||
|
# // put your setup code here, to run once:
|
||||||
|
# Serial.begin(19200);
|
||||||
|
# }
|
||||||
|
#
|
||||||
|
# void loop() {
|
||||||
|
# // put your main code here, to run repeatedly:
|
||||||
|
# if (Serial.available()) {
|
||||||
|
# // Read the most recent byte
|
||||||
|
# byte byteRead = Serial.read();
|
||||||
|
# // ECHO the value that was read
|
||||||
|
# Serial.write(byteRead);
|
||||||
|
# }
|
||||||
|
# }
|
||||||
|
|
||||||
|
# 3) Run the following script in OpenMV IDE:
|
||||||
|
|
||||||
|
import time
|
||||||
|
from pyb import UART
|
||||||
|
|
||||||
|
# UART 3, and baudrate.
|
||||||
|
uart = UART(3, 19200)
|
||||||
|
|
||||||
|
while(True):
|
||||||
|
uart.write("Hello World!\n")
|
||||||
|
if (uart.any()):
|
||||||
|
print(uart.read())
|
||||||
|
time.sleep(1000)
|
||||||
@ -6,9 +6,10 @@ import time
|
|||||||
from pyb import Pin, Timer
|
from pyb import Pin, Timer
|
||||||
|
|
||||||
tim = Timer(4, freq=1000) # Frequency in Hz
|
tim = Timer(4, freq=1000) # Frequency in Hz
|
||||||
# Generate a 1KHz square wave on TIM4 with 50% and 75% duty cycles on channels 1 and 2, respectively.
|
# Generate a 1KHz square wave on TIM4 with 50%, 75% and 50% duty cycles on channels 1, 2 and 3 respectively.
|
||||||
ch1 = tim.channel(1, Timer.PWM, pin=Pin("P7"), pulse_width_percent=50)
|
ch1 = tim.channel(1, Timer.PWM, pin=Pin("P7"), pulse_width_percent=50)
|
||||||
ch2 = tim.channel(2, Timer.PWM, pin=Pin("P8"), pulse_width_percent=75)
|
ch2 = tim.channel(2, Timer.PWM, pin=Pin("P8"), pulse_width_percent=75)
|
||||||
|
ch3 = tim.channel(3, Timer.PWM, pin=Pin("P9"), pulse_width_percent=50)
|
||||||
|
|
||||||
while (True):
|
while (True):
|
||||||
time.sleep(1000)
|
time.sleep(1000)
|
||||||
@ -7,13 +7,16 @@ from pyb import Servo
|
|||||||
|
|
||||||
s1 = Servo(1) # P7
|
s1 = Servo(1) # P7
|
||||||
s2 = Servo(2) # P8
|
s2 = Servo(2) # P8
|
||||||
|
s3 = Servo(3) # P9
|
||||||
|
|
||||||
while(True):
|
while(True):
|
||||||
for i in range(1000):
|
for i in range(1000):
|
||||||
s1.pulse_width(1000 + i)
|
s1.pulse_width(1000 + i)
|
||||||
s2.pulse_width(1999 - i)
|
s2.pulse_width(1999 - i)
|
||||||
|
s3.pulse_width(1000 + i)
|
||||||
time.sleep(10)
|
time.sleep(10)
|
||||||
for i in range(1000):
|
for i in range(1000):
|
||||||
s1.pulse_width(1999 - i)
|
s1.pulse_width(1999 - i)
|
||||||
s2.pulse_width(1000 + i)
|
s2.pulse_width(1000 + i)
|
||||||
|
s3.pulse_width(1999 - i)
|
||||||
time.sleep(10)
|
time.sleep(10)
|
||||||
|
|||||||
@ -3,10 +3,14 @@
|
|||||||
# This example shows how to use a timer for callbacks.
|
# This example shows how to use a timer for callbacks.
|
||||||
|
|
||||||
import time
|
import time
|
||||||
from pyb import Pin, Timer
|
from pyb import Pin, Timer, LED
|
||||||
|
|
||||||
def tick(timer): # we will receive the timer object when being called
|
blue_led = LED(3)
|
||||||
print("Timer callback")
|
|
||||||
|
# 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()
|
||||||
|
|
||||||
tim = Timer(4, freq=1) # create a timer object using timer 4 - trigger at 1Hz
|
tim = Timer(4, freq=1) # create a timer object using timer 4 - trigger at 1Hz
|
||||||
tim.callback(tick) # set the callback to our tick function
|
tim.callback(tick) # set the callback to our tick function
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user