Merge pull request #343 from kwagyeman/motor_shield

Add motor shield examples
This commit is contained in:
Ibrahim Abd Elkader 2018-04-22 22:37:57 +02:00 committed by GitHub
commit d2a462970b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 159 additions and 0 deletions

View File

@ -0,0 +1,67 @@
# Time Lapse Photos (Credit nedhorning)
#
# This example shows off how to take time lapse photos using your OpenMV
# Cam and using the RTC module along with a timer interrupt to achieve
# very low power operation.
#
# Note that if the USB is still plugged in when the camera is taking
# pictures it will run the bootloader each time. Please power the camera
# from something other than USB to not have the bootloader run.
import pyb, machine, sensor, image, pyb, os
# Create and init RTC object. This will allow us to set the current time for
# the RTC and let us set an interrupt to wake up later on.
rtc = pyb.RTC()
newFile = False
try:
os.stat('time.txt')
except OSError: # If the log file doesn't exist then set the RTC and set newFile to True
# datetime format: year, month, day, weekday (Monday=1, Sunday=7),
# hours (24 hour clock), minutes, seconds, subseconds (counds down from 255 to 0)
rtc.datetime((2018, 3, 9, 5, 13, 0, 0, 0))
newFile = True
# Extract the date and time from the RTC object.
dateTime = rtc.datetime()
year = str(dateTime[0])
month = '%02d' % dateTime[1]
day = '%02d' % dateTime[2]
hour = '%02d' % dateTime[4]
minute = '%02d' % dateTime[5]
second = '%02d' % dateTime[6]
subSecond = str(dateTime[7])
newName='I'+year+month+day+hour+minute+second # Image file name based on RTC
# Enable RTC interrupts every 10 seconds, camera will RESET after wakeup from deepsleep Mode.
rtc.wakeup(10000)
BLUE_LED_PIN = 3
sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.VGA)
sensor.skip_frames(time = 1000) # Let new settings take affect.
# Let folks know we are about to take a picture.
pyb.LED(BLUE_LED_PIN).on()
if(newFile): # If log file does not exist then create it.
with open('time.txt', 'a') as timeFile: # Write text file to keep track of date, time and image number.
timeFile.write('Date and time format: year, month, day, hours, minutes, seconds, subseconds' + '\n')
timeFile.write(newName + ',' + year + ',' + month + ',' + day + ',' + hour + ',' + minute + ',' + second + ',' + subSecond + '\n')
else:
with open('time.txt', 'a') as timeFile: # Append to date, time and image number to text file.
timeFile.write(newName + ',' + year + ',' + month + ',' + day + ',' + hour + ',' + minute + ',' + second + ',' + subSecond + '\n')
if not "images" in os.listdir(): os.mkdir("images") # Make a temp directory
# Take photo and save to SD card
img = sensor.snapshot()
img.save('images/' + newName, quality=90)
pyb.LED(BLUE_LED_PIN).off()
# Enter Deepsleep Mode (i.e. the OpenMV Cam effectively turns itself off except for the RTC).
machine.deepsleep()

View File

@ -0,0 +1,39 @@
# Motor Shield Power Driver Example
#
# This example shows off how to use your motor shield
# to control high current switches and so on. Since
# the motor shield provides two H-Bridge drivers it's
# able to provide 4 high current push-pull outputs.
# Each output can do up to 1A respectively.
import pyb
# These pins will be the ones we control to drive the
# H-Bridge sides as we like.
pinA = pyb.Pin('P3', pyb.Pin.OUT_PP, pyb.Pin.PULL_NONE)
pinB = pyb.Pin('P2', pyb.Pin.OUT_PP, pyb.Pin.PULL_NONE)
pinC = pyb.Pin('P1', pyb.Pin.OUT_PP, pyb.Pin.PULL_NONE)
pinD = pyb.Pin('P0', pyb.Pin.OUT_PP, pyb.Pin.PULL_NONE)
# Create a timer object running at 1KHz which which will power the
# PWM output on our OpenMV Cam. Just needs to be created once.
tim = pyb.Timer(4, freq=1000)
# These PWM channels will set the PWM percentage on the H-Bridge
# driver pair above. If you'd like to change the driver power
pinABPower = tim.channel(1, pyb.Timer.PWM, pin=pyb.Pin("P7"), pulse_width_percent=100)
pinCDPower = tim.channel(2, pyb.Timer.PWM, pin=pyb.Pin("P8"), pulse_width_percent=100)
while (True):
pyb.delay(1000)
pinA.value(0)
pinB.value(1)
pinC.value(0)
pinD.value(1)
pyb.delay(1000)
pinA.value(1)
pinB.value(0)
pinC.value(1)
pinD.value(0)

View File

@ -0,0 +1,53 @@
# Motor Shield PWM Example
#
# This example shows off how to control the motor shield on your
# OpenMV Cam. The motor shield is controlled by using the PYB module
# which lets you do PWM to control the speed and set digital I/O pin
# states. The motor shield needs 6 I/O pins for both motors.
import pyb
# These pins control our direction while the other PWN pins
# below control the speed. The direction for each motor
# is set by an H-Bridge where A0/1 are the two sides of
# one H-Bridge driver. B0/1 are another H-Bridge.
pinADir0 = pyb.Pin('P3', pyb.Pin.OUT_PP, pyb.Pin.PULL_NONE)
pinADir1 = pyb.Pin('P2', pyb.Pin.OUT_PP, pyb.Pin.PULL_NONE)
pinBDir0 = pyb.Pin('P1', pyb.Pin.OUT_PP, pyb.Pin.PULL_NONE)
pinBDir1 = pyb.Pin('P0', pyb.Pin.OUT_PP, pyb.Pin.PULL_NONE)
# Dir0/1 must be not equal to each other for forward or backwards
# operation. If they are equal then that's a brake operation.
# If they are not equal then the motor will spin one way other the
# other depending on it's hookup and the value of dir 0.
pinADir0.value(0)
pinADir1.value(1)
# Dir0/1 must be not equal to each other for forward or backwards
# operation. If they are equal then that's a brake operation.
# If they are not equal then the motor will spin one way other the
# other depending on it's hookup and the value of dir 0.
pinBDir0.value(0)
pinBDir1.value(1)
# Create a timer object running at 1KHz which which will power the
# PWM output on our OpenMV Cam. Just needs to be created once.
tim = pyb.Timer(4, freq=1000)
# Use the timer object to create two PWM outputs on the OpenMV Cam.
# These timers control the speed of the motors. You will be setting
# the PWM percentage of these timers repeatedly in your loop.
chA = tim.channel(1, pyb.Timer.PWM, pin=pyb.Pin("P7"))
chB = tim.channel(2, pyb.Timer.PWM, pin=pyb.Pin("P8"))
while (True):
for i in range(100):
pyb.delay(100)
chA.pulse_width_percent(i)
chB.pulse_width_percent(99-i)
for i in range(100):
pyb.delay(100)
chA.pulse_width_percent(99-i)
chB.pulse_width_percent(i)