PWM Motor Drive-Python
Python Motor Drive Tutorial
Learn how to command the motors using the Motor Hat provided in your Rover kit. This guide demonstrates how to send PWM commands to the Motor Hat over BLE using the Python Programming language. Visit here to download an IDE for your specific computer and operating system (OS).
Overview
In this guide you will learn how to manipulate and control the Rover based on the following basic concepts:
How to generate PWM commands for the ROVER.
Understand how the different PWM settings affect motor drive and direction.
The different parts of motordrive.py.
In order for this program to work, the host machine must have a working BLE radio that supports Bluetooth 4.0.
Motor Drive Python Files
If you choose to Download the following zip file and you will find 2 files:
bleak library
This library must be installed first https://github.com/hbldh/bleak
From the command prompt run: Run βpip install bleakβ
bluettoothinputpy.py
This module contains the underlying BLE communications.
You will need to identify the MAC address of your Motor Hat before running this program. When connecting to your sensor, the MAC addess follow MTH_. So in the exmple below, the sensor's name is MTH_CC037B92FDB3. As such, CC:03:7B:92:FD:B3 was added to line 5. Note that you must add in the colons in order for the code to work correctly.
Once it connects, you can input the PWM values and select which motor to drive in the console.
Program will run and terminate after 3 seconds based on line 35.
bluettoothinputpy.py
Breakdown
bluettoothinputpy.py
BreakdownLet's break down the contents of bluettoothinputpy.py
. For starters let's copy this code into your Python IDE.
IDE
Below is sample console output from bluetoothinputpy.py running in PowerShell, using Visual Studio Code. In this example, motor's 2 PWM value was set to 100 for 3 seconds.
Last updated
Was this helpful?