r/programmingrequests • • Sep 30 '16

[REQUEST] building tracked robot, need help programming controller/motor driver

I'm new to the whole thing. Building a FPV tracked robot. I'm 3d printing my own chassis, tracks and suspension. Will need help/input on running raspberry Pi3 with Bluetooth, sense hat, and motor controller hat. Any and all help appreciated

2 Upvotes

1 comment sorted by

1

u/[deleted] Oct 13 '16
    This might be a bit late but I'm somewhat familiar with motor controller hats on the RPi. It has been a long time since I programmed any externals for the Pi.

What exactly do you need? There are plenty of tutorials regarding sending signals to the GPIO pins. If you know the electronics and how to read a diagram, you should be able to do this on your own!

The hardest part I had with robotics was picking up KEYPRESSES in a loop. It's surprisingly hard to track if a key is being held down and then let go. Well -- hard for me. I think more intelligent people probably manage it quite well.

This was my manual motor controller:

import Tkinter as tk
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

GPIO.setup(26, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(6, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)

p = GPIO.PWM(26, 50)
q = GPIO.PWM(13, 50)
e = GPIO.PWM(6, 50)
r = GPIO.PWM(22, 50)

p.start(0)
q.start(0)
e.start(0)
r.start(0)



class App(object):
   
def __init__(self):
    self.right = False
    self.left = False
    self.up = False
    self.down = False
    self.speed = 1

def keyPressed(self,event):
    print("Stop")
    if event.keysym == 'Escape':
        root.destroy()
    elif event.keysym == 'Right':
        self.right = True
    elif event.keysym == 'Left':
        self.left = True
    elif event.keysym == 'Up':
        self.up = True
    elif event.keysym == 'Down':
        self.down = True
        
def keyReleased(self,event):
    if event.keysym == 'Right':
        self.right = False
        while self.speed > 3 and self.speed != 0:
           self.speed -= 3
           print("Right Engine Cool Down...", self.speed)

           p.ChangeDutyCycle(self.speed)
           time.sleep(.02)

        p.ChangeDutyCycle(0)
           
    elif event.keysym == 'Left':
        self.left = False
        while self.speed > 3 and self.speed != 0:
           self.speed -= 3
           print("Left Engine Cool Down...", self.speed)

           e.ChangeDutyCycle(self.speed)
           time.sleep(.02)

        e.ChangeDutyCycle(0)
           
    elif event.keysym == 'Up':
        self.up = False
        while self.speed > 3 and self.speed != 0:
           self.speed -= 3
           print("Sync Engine Cool Down...", self.speed)

           e.ChangeDutyCycle(self.speed) and p.ChangeDutyCycle(self.speed)
           time.sleep(.02)

        e.ChangeDutyCycle(0) and p.ChangeDutyCycle(0)
           
    elif event.keysym == 'Down':
        self.down = False
        while self.speed > 3 and self.speed != 0:
           self.speed -= 3
           print("Reverse Engine Cool Down...", self.speed)

           q.ChangeDutyCycle(self.speed) and r.ChangeDutyCycle(self.speed)
           time.sleep(.02)

        e.ChangeDutyCycle(0) and p.ChangeDutyCycle(0)            
        
def task(self):
    if self.right:
        print('Right')
        if(self.speed < 100):
           self.speed += 2
        print(self.speed)
        p.ChangeDutyCycle(self.speed)
        
    elif self.left:
        print('Left')
        if(self.speed < 100):
           self.speed += 2
        print(self.speed)
        e.ChangeDutyCycle(self.speed)
        
    elif self.up:
        print('Forward')
        if(self.speed < 100):
           self.speed += 2
        print(self.speed)
        p.ChangeDutyCycle(self.speed) and e.ChangeDutyCycle(self.speed)
        
    elif self.down:
        print('Backward')
        if(self.speed < 100):
           self.speed += 2
        print(self.speed)
        e.ChangeDutyCycle(self.speed) and r.ChangeDutyCycle(self.speed)
        
    root.after(20,self.task)

application = App()
root = tk.Tk()
print( "Press arrow key (Escape key to exit):" )

root.bind_all('<Key>', application.keyPressed)
root.bind_all('<KeyRelease>', application.keyReleased)
root.after(20,application.task)

root.mainloop()

Obviously change the GPIO pins for your controller.

I also made one which was controllable from an accelerometer:

#!/usr/bin/python
import RPi.GPIO as GPIO
import smbus
import math
import time

#init GPIO

GPIO.setmode(GPIO.BCM)

GPIO.setup(26, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(6, GPIO.OUT)
GPIO.setup(5, GPIO.OUT)

p = GPIO.PWM(26, 50)
q = GPIO.PWM(13, 50)
e = GPIO.PWM(6, 50)
r = GPIO.PWM(5, 50)
    
p.start(0)
q.start(0)
e.start(0)
r.start(0)

YPspeed = 0
YQspeed = 0
XPspeed = 0
XQspeed = 0

# Power management registers
power_mgmt_1 = 0x6b
power_mgmt_2 = 0x6c

def read_byte(adr):
    return bus.read_byte_data(address, adr)

def read_word(adr):
    high = bus.read_byte_data(address, adr)
    low = bus.read_byte_data(address, adr+1)
    val = (high << 8) + low
    return val

def read_word_2c(adr):
    val = read_word(adr)
    if (val >= 0x8000):
        return -((65535 - val) + 1)
    else:
        return val

def dist(a,b):
    return math.sqrt((a*a)+(b*b))

def get_y_rotation(x,y,z):
    radians = math.atan2(x, dist(y,z))
    return -math.degrees(radians)

def get_x_rotation(x,y,z):
    radians = math.atan2(y, dist(x,z))
    return math.degrees(radians)

bus = smbus.SMBus(1) #for Revision 2 boards
address = 0x68       # This is the address value read via the i2cdetect command

# Now wake the 6050 up as it starts in sleep mode
bus.write_byte_data(address, power_mgmt_1, 0)



while True:
    print "gyro data"
    print "---------"

    gyro_xout = read_word_2c(0x43)
    gyro_yout = read_word_2c(0x45)
    gyro_zout = read_word_2c(0x47)

    print "gyro_xout: ", gyro_xout, " scaled: ", (gyro_xout / 131)
    print "gyro_yout: ", gyro_yout, " scaled: ", (gyro_yout / 131)
    print "gyro_zout: ", gyro_zout, " scaled: ", (gyro_zout / 131)

    print
    print "accelerometer data"
    print "------------------"

    accel_xout = read_word_2c(0x3b)
    accel_yout = read_word_2c(0x3d)
    accel_zout = read_word_2c(0x3f)

    accel_xout_scaled = accel_xout / 16384.0
    accel_yout_scaled = accel_yout / 16384.0
    accel_zout_scaled = accel_zout / 16384.0

    print "accel_xout: ", accel_xout, " scaled: ", accel_xout_scaled
    print "accel_yout: ", accel_yout, " scaled: ", accel_yout_scaled
    print "accel_zout: ", accel_zout, " scaled: ", accel_zout_scaled
    
    print "x rotation: " , get_x_rotation(accel_xout_scaled, accel_yout_scaled, accel_zout_scaled)
    print "y rotation: " , get_y_rotation(accel_xout_scaled, accel_yout_scaled, accel_zout_scaled)

    #Y direction
    if accel_yout < 5000:
    if YPspeed >= 2 and YPspeed != 0:
        YPspeed -= 2
    
    if XPspeed < 60:
        XPspeed += 2
        
    if accel_yout < 10000 and XPspeed < 100:
        XPspeed += 2
    
    print("Right Engine Cool Down...", YPspeed)
    print("Left Engine Increase... ", XPspeed)

    p.ChangeDutyCycle(YPspeed)
    e.ChangeDutyCycle(XPspeed)
        
    time.sleep(.02)



if accel_yout > 5000 and YPspeed < 100:
    print('Right Engine Increase... ', YPspeed)
    print('Left Engine Decrease... ', XPspeed)
    if YPspeed < 60:
        YPspeed += 2
    
    if accel_yout > 10000 and YPspeed < 100:
            YPspeed += 2
        
    if XPspeed >= 2:
        XPspeed -= 2
    print(YPspeed)

    p.ChangeDutyCycle(YPspeed) 
    e.ChangeDutyCycle(XPspeed)
    
    accel_yout = read_word_2c(0x3d)

    time.sleep(.02)


time.sleep(.02)