Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Christina Mezmur
Published © GPL3+

Remote Control Car w/ PocketBeagle and Arduino

Have you ever wanted to use a PocketBeagle? And build a remote car? Well, I have! Here is how you can too!

IntermediateFull instructions provided10 hours9,022
Remote Control Car w/ PocketBeagle and Arduino

Things used in this project

Hardware components

PocketBeagle
BeagleBoard.org PocketBeagle
×1
Arduino UNO
Arduino UNO
×1
Bluetooth Low Energy (BLE) Module (Generic)
×1
Spark-fun Multi-Chassis Basic 4WD Kit
×1
Dual H-Bridge motor drivers L293D
Texas Instruments Dual H-Bridge motor drivers L293D
×1
Anker PowerCore+ mini, 3350mAh Lipstick-Sized Portable Charger (3rd Generation, Premium Aluminum Power Bank), One of the Most Compact External Batteries
×1
Resistor 10k ohm
Resistor 10k ohm
×4
Resistor 4.75k ohm
Resistor 4.75k ohm
×4
AA Batteries
AA Batteries
×2
9V battery (generic)
9V battery (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE
Adafruit Bluefruit LE Connect

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Fritzing Diagram

Code

Cloud 9: Python RC_car

Python
import time
import random

# ------------------------------------------------------------------------
# Constants
# ------------------------------------------------------------------------

# Peripheral path
GPIO_BASE_PATH               = "/sys/class/gpio"
ADC_BASE_PATH                = "/sys/bus/iio/devices/iio:device0"

# GPIO direction (if it is outputting or inputting a signal to the GPIO)
IN                           = True
OUT                          = False

# GPIO output state
LOW                          = "0"
HIGH                         = "1"

#Motor 1 
motor_1_ln1 				 = (0,30) #gpio30 P2_P5
motor_1_ln2					 = (0,31) #gpio31 P2_P7
motor_1_en1  				 = (1,28) #gpio60 P2_P8

#Motor 2
motor_2_ln4					 = (1,25) #gpio57 P2_P6
motor_2_ln3					 = (1,26) #gpio58 P2_P4
motor_2_en3					 = (1,27) #gpio59 P2_P2

#Motor 3
motor_3_ln1					 = (1,10) #gpio42 P1_P32
motor_3_ln2					 = (1,11) #gpio43 P1_P30
motor_3_en1					 = (0,26) #gpio26 P1_34

#Motor4
motor_4_ln4					 = (3,15) #gpio111 P1_33
motor_4_ln3					 = (2,24) #gpio88 P1_35
motor_4_en3					 = (3,14) #gpio110 P1_36

ard1                        =(3,20) #going to ard6
ard2                        =(3,17) #going to ard5
ard3                        =(3,16) #going to ard3
ard4                        =(3,19) #going to ard2


motor1						 = (motor_1_ln1, motor_1_ln2, motor_1_en1)
motor2						 = (motor_2_ln3, motor_2_ln4, motor_2_en3)
motor3						 = (motor_3_ln1, motor_3_ln2, motor_3_en1)
motor4						 = (motor_4_ln3, motor_4_ln4, motor_4_en3)


# ------------------------------------------------------------------------
# Global variables
# ------------------------------------------------------------------------

start_time                    = 30.0              # Start time (in seconds)


# ------------------------------------------------------------------------
# GPIO / ADC access library
# ------------------------------------------------------------------------
import os

def gpio_setup(gpio, direction, default_value=False):
    """Setup GPIO pin
    
      * Test if GPIO exists; if not create it
      * Set direction
      * Set default value
    """
    gpio_number = str((gpio[0] * 32) + gpio[1])
    path        = "{0}/gpio{1}".format(GPIO_BASE_PATH, gpio_number)
    
    if not os.path.exists(path):
        # "echo {gpio_number} > {GPIO_BASE_PATH}/export"
        print("Create GPIO: {0}".format(gpio_number))
        with open("{0}/export".format(GPIO_BASE_PATH), 'w') as f:
            f.write(gpio_number)
    
    if direction:
        # "echo in > {path}/direction"
        with open("{0}/direction".format(path), 'w') as f:
            f.write("in")
    else:
        # "echo out > {path}/direction"
        with open("{0}/direction".format(path), 'w') as f:
            f.write("out")
        
    if default_value:
        # "echo {default_value} > {path}/value"
        with open("{0}/value".format(path), 'w') as f:
            f.write(default_value)
    
# End def


def gpio_set(gpio, value):
    """Set GPIO ouptut value."""
    gpio_number = str((gpio[0] * 32) + gpio[1])
    path        = "{0}/gpio{1}".format(GPIO_BASE_PATH, gpio_number)
    
    # "echo {value} > {path}/value"
    with open("{0}/value".format(path), 'w') as f:
        f.write(value)

# End def


def gpio_get(gpio):
    """Get GPIO input value."""
    gpio_number = str((gpio[0] * 32) + gpio[1])
    path        = "{0}/gpio{1}".format(GPIO_BASE_PATH, gpio_number)
    
    # "cat {path}/value"
    with open("{0}/value".format(path), 'r') as f:
        out = f.read()
    
    return float(out)

# End def

    
def setforward(motor):
	gpio_set(motor[0],HIGH)
	gpio_set(motor[1],LOW)
	
#End def
def setbackward(motor):
	gpio_set(motor[0],LOW)
	gpio_set(motor[1],HIGH)
	
#End def

	
def move():
	gpio_set(motor1[2],HIGH)
	gpio_set(motor2[2],HIGH)
	gpio_set(motor3[2],HIGH)
	gpio_set(motor4[2],HIGH)

#End def
	
def stop():
	gpio_set(motor1[2],LOW)
	gpio_set(motor2[2],LOW)
	gpio_set(motor3[2],LOW)
	gpio_set(motor4[2],LOW)

#End def

def setleft():

	"""move the left wheels forward"""
	setforward(motor1)
	setforward(motor3)
	
	"""move the right wheels backward"""
	setbackward(motor2)
	setbackward(motor4)
	
#End def

def setright():
	"""move the left wheels backward"""
	
	setbackward(motor1)
	setbackward(motor3)
	
	"""move the right wheels forward"""
	setforward(motor2)
	setforward(motor4)

#End def


def setupcar():
	"""Setup gpio"""
	gpio_setup(motor_1_ln1, OUT, LOW)
	gpio_setup(motor_1_ln2, OUT, LOW)
	gpio_setup(motor_1_en1, OUT, LOW)

	"""motor 2"""
	gpio_setup(motor_2_ln3, OUT, LOW)
	gpio_setup(motor_2_ln4, OUT, LOW)
	gpio_setup(motor_2_en3, OUT, LOW)
	
	"""motor 3"""
	gpio_setup(motor_3_ln1, OUT, LOW)
	gpio_setup(motor_3_ln2, OUT, LOW)
	gpio_setup(motor_3_en1, OUT, LOW)
	
	"""motor 4"""
	gpio_setup(motor_4_ln3, OUT, LOW)
	gpio_setup(motor_4_ln4, OUT, LOW)
	gpio_setup(motor_4_en3, OUT, LOW)
	
	gpio_setup(ard1, IN)
	gpio_setup(ard2, IN)
	gpio_setup(ard3, IN)
	gpio_setup(ard4, IN)
	
#End def

def drive_forward(drivetime):
    setforward(motor1)
    setforward(motor2)
    setforward(motor3)
    setforward(motor4)
    move()
    time.sleep(drivetime)
    stop()
  	
#End def

def drive_backward(drivetime):
    setbackward(motor1)
    setbackward(motor2)
    setbackward(motor3)
    setbackward(motor4)
    move()
    time.sleep(drivetime)
    stop()
  	
#End def
  	
def drive_right(drivetime):
    setright()
    move()
    time.sleep(drivetime)
    stop()
#End def

def drive_left(drivetime):
    setleft()
    move()
    time.sleep(drivetime)
    stop()
#End def

# ------------------------------------------------------------------------
# Main script
# ------------------------------------------------------------------------

if __name__ == '__main__':
    setupcar()
    while (True):
        if gpio_get(ard1):
           # print("Forward")
            drive_forward(0.5)
        if gpio_get(ard2):
            #print("Backward")
            drive_backward(0.5)
        if gpio_get(ard3):
            #print("Right")
            drive_right(0.5)
        if gpio_get(ard4):
           # print("Left")
            drive_left(0.5)
    


  	

Cloud9: Config Pins

Python
#!/bin/bash
# --------------------------------------------------------------------------
# Motion Detection Demo Pin Configuration
# --------------------------------------------------------------------------
# Copyright 2017-18 Octavo Systems LLC
# Redistribution and use in source and binary forms, with or without modification, 
# are permitted provided that the following conditions are met:

# 1. Redistributions of source code must retain the above copyright notice, this 
   # list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice, 
   # this list of conditions and the following disclaimer in the documentation and/or 
   # other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the names of its contributors may be
   # used to endorse or promote products derived from this software without specific 
   # prior written permission.

   # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
   # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
   # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
   # IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
   # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
   # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
   # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
   # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
   # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
   # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# 
# --------------------------------------------------------------------------
# Auto-run script
#
# Add to crontab:
#   - In code directory <code>
#     1) mkdir logs
#     2) sudo crontab -e
#     3) @reboot sh <code>/run_motion_demo.sh > <code>/logs/cronlog 2>&1
#
# Reboot the PocketBeagle to then auto-run the program
#
# --------------------------------------------------------------------------

/var/lib/cloud9/RCCar/config_pins.sh
python /var/lib/cloud9/RCCar/remotecontrolcar.py

Credits

Christina Mezmur

Christina Mezmur

1 project • 10 followers
I am a Mechanical Engineer student at Rice University in a Practical Electrical Engineering course.

Comments