I said I wouldn't start from the very beginning, skipping the learning GPIOs and what not. Apparently, I am also going to skip my ugly first steps into using an H-bridge controller. And it was ugly. I had tried making my own l298n controller, but wasn't able to get it working. I purchased a cheap l298n from China off of eBay. When it finally arrived, I hooked up straight away to my two scavenged motors and a scavenged two AA battery pack. I could run one or the other, but not both. So, off to RadioShack to grab a power pack. From this I was able to get movement. I had always planned to run a tracked robot, and while I was at a local Hobby Town (they specialize in RC among other things), I found a Tamiya track set, as well as a Tamiya Double Gearbox. This allowed me to put together the above setup. I will discuss the gearbox and tracks in a later post. As you can see, I was still using a breakout and a breadboard. This is a great place to test on, but I scrapped it later in favor of cleaner wiring. You can also see some ribbon cable slapped in there. From my early days of computer building, I have plenty of these IDE cables lying around. However, these were just as messy, and when I finally received my male to female and female to female jumpers, I swapped them out. At some point I may come back to a full plug to easier add and remove my Pi.
The H-bridge is fairly easy to hook up. Choose 4 GPIO pins, in my case 17, 18, 21, and 22. Connect these to In 1,2,3, and 4. I left the jumpers on the enable pins, as I wasn't up to PWM for this yet. Then connect the ground of the H-bridge to the Pi AND your battery pack. Connect the batteries positive(red) to the VCC. I had attached the 5v, but a larger battery pack reset my Pi. Then connect the outs to your motors. One set to one motor, the other to the other motor. Order is less important here, but 1 and 4 to the left side of the motors and 2 and 3 to the other sides makes it easier to spin them correctly in the code.
At some point I should learn to make wiring diagrams, but at the moment, that is not today.
Here is my initial code to run things:
import RPi.GPIO as GPIO
import time
in1_pin = 17
in2_pin = 18
in3_pin = 22
in4_pin = 23
x = True
GPIO.setmode(GPIO.BCM)
GPIO.setup(in1_pin, GPIO.OUT)
GPIO.setup(in2_pin, GPIO.OUT)
GPIO.setup(in3_pin, GPIO.OUT)
GPIO.setup(in4_pin, GPIO.OUT)
GPIO.output(in1_pin, False)
GPIO.output(in2_pin, False)
GPIO.output(in3_pin, False)
GPIO.output(in4_pin, False)
def clockwise():
GPIO.output(in1_pin, True)
GPIO.output(in2_pin, False)
GPIO.output(in3_pin, True)
GPIO.output(in4_pin, False)
print("Clockwise")
def counter_clockwise():
GPIO.output(in1_pin, False)
GPIO.output(in2_pin, True)
GPIO.output(in3_pin, False)
GPIO.output(in4_pin, True)
print("Counter clockwise")
while x:
cmd = raw_input("Command, f/r 0..9, E.g. f5 :")
direction = cmd[0]
if direction == "f": #"Forward"
clockwise() #I used clockwise/counter to test direction, then match
elif direction == "s": #Stop/exit loop
x = False
elif direction == "r": #"Reverse"
counter_clockwise()
else:
print ("Invalid input")
speed = int(cmd[1]) * 10
GPIO.cleanup() #Needed to release the GPIO and stop motors
Fairly simple. I used pins 17 and 18 for one side and 22 and 23 for the other. I then had a simple command input to move forward and back. As my code was an extension from a previous attempt to build my own bridge using just the L298n chip, I also had input for speed. I removed all of that code to nix confusion, but that part of the input is still there. At this point and for a bit longer, I won't be using PWM. I am going full on, full off, and just using the enable jumpers that came on the L298N bridge for 100% PWM. When I get my robot to working remotely, then I will worry about PWM. For right now, I am happy that I can move my wheels forward and back. Be sure to note which way the motors spin, so that you can later alter the code to make the robot move how you want. Mine is a little backwards, and 2 and 4 are forward, while 1 and 3 are backwards. The way to fix this in the hardware would be to switch the motor connections, but I have just chosen to code them that way.
No comments:
Post a Comment