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:
iabdalkader 2019-09-30 16:44:54 +02:00
parent cba3d7ee70
commit f04b18fe6f
6 changed files with 72 additions and 33 deletions

View File

@ -8,7 +8,7 @@
# OpenMV Cam Slave Select (P3) - Arduino Uno SS (10)
# OpenMV Cam Ground - Arduino Ground
import pyb, ustruct
import pyb, ustruct, time
text = "Hello World!\n"
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).
# 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!!!
#
# 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.
# 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)
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
# Arduino starts to poll the OpenMV Cam for data. Otherwise the SPI byte framing gets messed up,
# and etc. So, keep the Arduino in reset until the OpenMV Cam is "Waiting for Arduino...".
while(True):
while(pin.value()): pass
# NSS callback.
def nss_callback(line):
global spi, data
try:
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:
pass # Don't care about errors - so pass.
# 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]".
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
@ -67,7 +62,7 @@ while(True):
# #define SS_PIN 10
# #define BAUD_RATE 19200
# #define CHAR_BUF 128
#
#
# void setup() {
# pinMode(SS_PIN, OUTPUT);
# Serial.begin(BAUD_RATE);
@ -77,24 +72,22 @@ while(True):
# SPI.setDataMode(SPI_MODE0);
# delay(1000); // Give the OpenMV Cam time to bootup.
# }
#
#
# void loop() {
# int32_t temp = 0;
# int32_t len = 0;
# char buff[CHAR_BUF] = {0};
# digitalWrite(SS_PIN, LOW);
# delay(1); // Give the OpenMV Cam some time to setup to send data.
#
#
# if(SPI.transfer(1) == 85) { // saw sync char?
# SPI.transfer(&temp, 4); // get length
# int zero_legnth = 4 + ((temp + 1) % 4);
# if (temp) {
# SPI.transfer(&buff, min(temp, CHAR_BUF));
# temp -= min(temp, CHAR_BUF);
# SPI.transfer(&len, 4); // get length
# if (len) {
# SPI.transfer(&buff, min(len, CHAR_BUF));
# len -= min(len, CHAR_BUF);
# }
# while (temp--) SPI.transfer(0); // eat any remaining bytes
# while (zero_legnth--) SPI.transfer(0); // eat zeros.
# while (len--) SPI.transfer(0); // eat any remaining bytes
# }
#
#
# digitalWrite(SS_PIN, HIGH);
# Serial.print(buff);
# delay(1); // Don't loop to quickly.

View 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)

View File

@ -6,9 +6,10 @@ import time
from pyb import Pin, Timer
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)
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):
time.sleep(1000)
time.sleep(1000)

View File

@ -7,13 +7,16 @@ from pyb import Servo
s1 = Servo(1) # P7
s2 = Servo(2) # P8
s3 = Servo(3) # P9
while(True):
for i in range(1000):
s1.pulse_width(1000 + i)
s2.pulse_width(1999 - i)
s3.pulse_width(1000 + i)
time.sleep(10)
for i in range(1000):
s1.pulse_width(1999 - i)
s2.pulse_width(1000 + i)
s3.pulse_width(1999 - i)
time.sleep(10)

View File

@ -3,13 +3,17 @@
# This example shows how to use a timer for callbacks.
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
print("Timer callback")
blue_led = LED(3)
# 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.callback(tick) # set the callback to our tick function
while (True):
time.sleep(1000)
time.sleep(1000)