Control MDDS30 In Serial Simplified Mode Using Raspberry Pi

Need to program SmartDrive using Raspberry Pi? Let's try.

Introduction

In previous tutorial, I have shared on how to program USB port in Raspberry Pi and communicate with Maker UNO using serial communication. Based on that lesson, this time we will try to control MDDS30 in Serial Simplified mode using Raspberry Pi.

Video

This video will show you how to control SmartDriveDuo-30 (MDDS30) in Serial Simplified mode using Raspberry Pi.

Hardware Preparation

This is the list of items used in the video.

Sample Program

This is the sample code for controlling MDDS30 in Serial Simplified mode using Raspberry Pi.

from gpiozero import LED, Button, Buzzer
from time import sleep
import serial
led1 = LED(17)
led2 = LED(18)
sw1 = Button(21)
sw2 = Button(16)
sw3 = Button(20)
buzzer = Buzzer(26)
serialPort = serial.Serial("/dev/ttyUSB0", 9600, timeout=0.5)
def sw1Pressed():
global speed
global currentSpeed
global motorLeft
global motorRight
global motorDirection
if speed < 63:
speed = speed + 5
currentSpeed = currentSpeed + 5
if speed == 65:
speed = speed 2
currentSpeed = currentSpeed 2
motorLeft = currentSpeed
motorRight = currentSpeed | 128
if speed == 63:
led1.on()
buzzer.beep(0.1, 0.1, 2)
def sw2Pressed():
global speed
global currentSpeed
global motorLeft
global motorRight
global motorDirection
if speed > 0:
if speed == 63:
speed = speed 3
currentSpeed = currentSpeed 3
else:
speed = speed 5
currentSpeed = currentSpeed 5
motorLeft = currentSpeed
motorRight = currentSpeed | 128
if speed == 0:
led1.off()
buzzer.beep(0.1, 0.1, 2)
def sw3Pressed():
global speed
global currentSpeed
global motorLeft
global motorRight
global motorDirection
if speed != 0:
led2.toggle()
buzzer.beep(0.1, 0.1, 1)
motorDirection = not motorDirection
if motorDirection == False:
motorLeft = motorLeft & 191
motorRight = motorRight & 191
else:
motorLeft = motorLeft | 64
motorRight = motorRight | 64
currentSpeed = motorLeft
sw1.when_pressed = sw1Pressed
sw2.when_pressed = sw2Pressed
sw3.when_pressed = sw3Pressed
packet = bytearray()
speed = 0
currentSpeed = 0
previousSpeed = 0
motorLeft = 0
motorRight = 0
motorDirection = False
try:
while True:
if currentSpeed != previousSpeed:
print("Motor Speed: {}, Left: {}, Right: {}".format(speed, motorLeft, motorRight))
previousSpeed = currentSpeed
packet.append(motorLeft)
packet.append(motorRight)
serialPort.write(packet)
packet.clear()
except KeyboardInterrupt:
led1.off()
led2.off()
buzzer.off()

Thank You

References:

  1. Python Serial library – PySerial
  2. Library to control GPIO on Raspberry Pi – gpiozero
  3. Motor Driver SmartDriveDuo-30 User’s Manual

Thanks for reading this tutorial. If you have any technical inquiry, please post at Cytron Technical Forum.

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

Getting Started with Robotic Operating System (ROS) and JetRacer AI Kit
Barrier Gate with micro:bit and MakeCode
Play MP3 File on Maker Pi Pico Using CircuitPython
Remote Jetson Nano using SSH with Visual Studio Code
3.3V Serial LCD with Raspberry Pi Pico via SPI or I2C
Tutorials of Cytron Technologies Scroll to Top