We will be updating CodeRover with Raspberry Pi sample Codes here. For now, we have 3 sample projects to get you started with the CodeRover Raspberry Pi HAT module.
In the first sample code, we will go through how to use the Raspberry Pi HAT to control a motor connected to the 2-Pin JST connector on the Pi HAT.
The 2-Pin JST connector on the Pi HAT is mapped to Raspberry Pi's 10 and 11 GPIO. So at the beginning of the program, we need to set 10 and 11 as GPIO.OUT pin with initial LOW voltage.
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
#pin 10 and 11 on Raspberry PI HAT is OB1 OA1
GPIO.setup(10, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(11, GPIO.OUT, initial=GPIO.LOW)
blinks = 0
#this while loop will repeat 4 times
while(blinks < 5):
#motor connected to OB1&OA1 spin clockwise
GPIO.output(10, GPIO.LOW)
GPIO.output(11, GPIO.HIGH)
time.sleep(1.0)
#motor connected to OB1&OA1 spin counter-clockwise
GPIO.output(10, GPIO.HIGH)
GPIO.output(11, GPIO.LOW)
time.sleep(1.0)
#motor stop spinning
GPIO.output(10, GPIO.LOW)
GPIO.output(11, GPIO.LOW)
time.sleep(1.0)
#increment variable blinks
blinks = blinks + 1
print('spin stop')
GPIO.cleanup()
In this sample program, we are using a while loop to increase the variable called blinks while controlling motor movements. Every time the while loop performs, the motor connected to the core will spin clockwise for 1 second and spin counter-clockwise for 1 second then stop. The variable blinks start at 0 and on the 5th iteration, it will no longer be less than 5, so the while loop will loop 4 times.
In the end, remember to use the cleanup function to free up all the ports we have used in the program on the Raspberry Pi. It resets used GPIOs back to input mode to prevent any potential short-circuit damage to Raspberry Pi.
Finally, if you want to control motors on the Core, make sure to connect your Raspberry Pi to Raspberry Pi HAT and use the 12p cord to connect to CodeRover Core. CodeRover Core's motors are mapped to Raspbberry Pi's 17&18 pin for motor 1 and 23&24 pin for motor 2.
In the second sample program, we will use the servo pin connecter on Raspberry Pi HAT to control an SG90 servo motor.
For this exercise, we connected the signal pin of the servo motor to pin 16 on the Raspberry Pi HAT. We will use this pin to send PWM signals to the servo motor. You can read more about how to use PWM to control servo motors in this Electronic Hub article.
From the information on the SG90 servo motor, we can see that the servo motor expects to see a pulse on the PWM pin every 20ms which is 50Hz. The range of the duty cycle needs to be within 1-2 ms. The Servo motor will go to 0 degrees at 1 ms and 180 degrees at 2 ms.
0 degrees = (1/20)*100 = 5% and for 180 degrees = (2/20)*100 = 10%. We can now use min and max duty, 5% and 10%, to build a function that translates degrees to duty cycle.
import RPi.GPIO as GPIO
import time
import signal
import atexit
atexit.register(GPIO.cleanup)
minDuty = 5
maxDuty = 10
def degToDuty(deg):
return (deg-0) * (maxDuty - minDuty) / 180 + minDuty
#servo's signal is connected to Pin 16
servopin=16
GPIO.setmode(GPIO.BCM)
GPIO.setup(servopin,GPIO.OUT,initial=False)
#sg90 servo expect to see a pulse on their PWM pin every 20ms
#1/0.02S = 50KHZ
p=GPIO.PWM(servopin,50)
#initial duty cycle is 0 means no output.
p.start(0)
time.sleep(2)
GPIO.setwarnings(False)
while True:
#move from 0 to 180 degrees
for i in range(0,181,10):
#convert 0 to 180 degrees to PWM using this formula
p.ChangeDutyCycle(degToDuty(i))
time.sleep(0.02)
p.ChangeDutyCycle(0)
time.sleep(0.2)
#move from 180 to 0 degrees
for i in range(181,0,-10):
p.ChangeDutyCycle(degToDuty(i))
time.sleep(0.02)
p.ChangeDutyCycle(0)
time.sleep(0.2)
GPIO.cleanup()
For the last exercise, we will test an IR sensor module on Raspberry Pi HAT. We will connect the IR sensor module to Raspberry Pi HAT with its signal pin attached to pin 13.
In order to visualize that the IR sensor is detecting something, we will also connect a servo motor to pin 16. When the IR sensor detects our hand, the servo will spin to 180 degrees otherwise servo will spin to 0 degrees.
import RPi.GPIO as GPIO
import time
import signal
import atexit
atexit.register(GPIO.cleanup)
minDuty = 5
maxDuty = 10
def degToDuty(deg):
return (deg-0) * (maxDuty - minDuty) / 180 + minDuty
#servo's signal is connected to Pin 16
servopin=16
sensorState=0
GPIO.setmode(GPIO.BCM)
GPIO.setup(servopin,GPIO.OUT,initial=False)
GPIO.setup(13, GPIO.IN)
#sg90 servo sxpect to see a pulse on their PWM pin every 20ms
#1/0.02S = 50KHZ
p=GPIO.PWM(servopin,50)
#initial duty cycle is 0 means no output.
p.start(0)
time.sleep(2)
GPIO.setwarnings(False)
while True:
if (GPIO.input(13) == GPIO.LOW):
p.ChangeDutyCycle(degToDuty(0))
time.sleep(0.02)
p.ChangeDutyCycle(0)
time.sleep(0.2)
elif(GPIO.input(13) == GPIO.HIGH):
p.ChangeDutyCycle(degToDuty(180))
time.sleep(0.02)
p.ChangeDutyCycle(0)
time.sleep(0.2)
GPIO.cleanup()
Here is a video of the final result, enjoy!
Comentarios