Created
March 6, 2019 10:26
-
-
Save IdrisCytron/8084f7f6b0d15861bf4f0f948b623941 to your computer and use it in GitHub Desktop.
Controlling mobile robot by using Blynk Joystick widget, Maker Drive, Raspberry Pi Zero WH, Maker pHAT.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from gpiozero import LED, Button, Buzzer | |
import pigpio | |
import BlynkLib | |
from time import sleep | |
pi = pigpio.pi() | |
BLYNK_AUTH = '923ae9c3e88e45a5a1ec139dbcde9f32' | |
# Initialize Blynk | |
blynk = BlynkLib.Blynk(BLYNK_AUTH) | |
sw1 = Button(21) | |
sw2 = Button(16) | |
sw3 = Button(20) | |
buzzer = Buzzer(26) | |
M1A = 17 | |
M1B = 18 | |
M2A = 27 | |
M2B = 22 | |
pi.set_mode(M1A, pigpio.OUTPUT) | |
pi.set_mode(M1B, pigpio.OUTPUT) | |
pi.set_mode(M2A, pigpio.OUTPUT) | |
pi.set_mode(M2B, pigpio.OUTPUT) | |
def valueMap(x, in_min, in_max, out_min, out_max): | |
return int((x-in_min) * (out_max-out_min) / (in_max-in_min) + out_min) | |
# Register Virtual Pins | |
@blynk.VIRTUAL_WRITE(0) | |
def v0_write_handler(value): | |
valueX = int(format(value[0])) | |
valueY = int(format(value[1])) | |
valueXMap = valueMap(valueX, 0, 255, -255, 255) | |
valueYMap = valueMap(valueY, 0, 255, -255, 255) | |
pwmLeft = valueYMap + valueXMap | |
if pwmLeft > 255: | |
pwmLeft = 255 | |
elif pwmLeft < -255: | |
pwmLeft = -255 | |
pwmRight = valueYMap - valueXMap | |
if pwmRight > 255: | |
pwmRight = 255 | |
elif pwmRight < -255: | |
pwmRight = -255 | |
motorSpeed(pwmLeft, pwmRight) | |
#print('Motor Left: {}'.format(pwmLeft)) | |
#print('Motor Right: {}'.format(pwmRight)) | |
def motorSpeed(speedLeft, speedRight): | |
if speedLeft > 0: | |
pi.set_PWM_dutycycle(M2A, speedLeft) | |
pi.set_PWM_dutycycle(M2B, 0) | |
else: | |
pi.set_PWM_dutycycle(M2A, 0) | |
pi.set_PWM_dutycycle(M2B, abs(speedLeft)) | |
if speedRight > 0: | |
pi.set_PWM_dutycycle(M1A, speedRight) | |
pi.set_PWM_dutycycle(M1B, 0) | |
else: | |
pi.set_PWM_dutycycle(M1A, 0) | |
pi.set_PWM_dutycycle(M1B, abs(speedRight)) | |
motorSpeed(0, 0) | |
buzzer.beep(0.1, 0.1, 1) | |
try: | |
while True: | |
blynk.run() | |
except KeyboardInterrupt: | |
motorSpeed(0, 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment