Tolo5star
Posts: 14
Joined: Mon May 05, 2014 8:35 am

Raspberry Pi wifi Keyboard controlled car

Mon May 05, 2014 9:56 am

So i have been trying to make a remote controlled car using raspberry pi and a wireless keyboard
i have referred to this video [Mod Removed link - video wrong]

but it teaches to use only one motor all my materials are similar to those used in the video including the l298n h bridge
Can anyone tell me how do i control 2 motors and use the keyboard to control them (wireless of course) If possible can give me the coding(in python )

Help will be appreciated !!!!!

Tarcas
Posts: 741
Joined: Thu Jan 09, 2014 5:38 am
Location: USA

Re: Raspberry Pi wifi Keyboard controlled car

Mon May 05, 2014 2:21 pm

Argh, that video again. It tells you to connect it wrong, and has already destroyed at least one Pi because of it. Don't follow his directions with respect to the Enable pins.

The two pins above ENA and ENB (each) are typically connected by a jumper. The upper one (away from the edge) is connected to the +5v rail. DO NOT connect this to your PI. EVER. The lower one, next to the edge, is the actual Enable pin. This you may connect to your Pi, but it's easier to just connect it to the adjacent 5v pin with a jumper.
Other than that his directions are good. IN1 and IN2 drive motor A. IN3 and IN4 drive motor B in the same way.
Apply voltage to one pin, and the associated motor will spin one direction. Apply it to the other pin for the same motor, and it will spin the other way.
If you want to do speed control, you can use PWM (Pulse Width Modulation) on either the Enable pin, or the IN pin. If you're jumpering the Enable pins, your only option is the IN pins, but you save 2 extra GPIOs for other purposes.

Coding is going to depend on your wiring. In order for anyone to even attempt to give you code for it, they'd have to know how you have it connected. Of course, that's assuming someone is inclined to do your work for you, which is unlikely. ;-)

Tolo5star
Posts: 14
Joined: Mon May 05, 2014 8:35 am

Re: Raspberry Pi wifi Keyboard controlled car

Tue May 06, 2014 6:40 am

Hey Tarcas please could u post a pic or a diagram for the connections I am 100% sure about your +5v explanation because my pi had power light on so it was working on the batteries . Thank you for your help

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Raspberry Pi wifi Keyboard controlled car

Tue May 06, 2014 7:40 am

Tolo5star wrote:Hey Tarcas please could u post a pic or a diagram for the connections I am 100% sure about your +5v explanation because my pi had power light on so it was working on the batteries . Thank you for your help
Do not connect a Pi gpio to the red ENABLE JUMPER pins.

http://abyz.co.uk/rpi/misc/mdb.png

If there is a jumper over the pink LOGIC POWER JUMPER do not connect anything to the LOGIC SUPPLY 5V screw terminal block,

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Raspberry Pi wifi Keyboard controlled car

Tue May 06, 2014 7:43 am

Tarcas wrote:Argh, that video again.
...
As the Foundation effectively endorse the tutorials by linking to them I have e-mailed info@raspberrypi.org asking them to get an engineer to check the video.

Tolo5star
Posts: 14
Joined: Mon May 05, 2014 8:35 am

Re: Raspberry Pi wifi Keyboard controlled car

Tue May 06, 2014 4:03 pm

Can you tell me which gpio pins should be connected to Enable A&B
If possible a diagram or a picture would be extremely useful
Thanks in advance

Tolo5star
Posts: 14
Joined: Mon May 05, 2014 8:35 am

Re: Raspberry Pi wifi Keyboard controlled car

Tue May 06, 2014 4:07 pm

And thnx Joan

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Raspberry Pi wifi Keyboard controlled car

Tue May 06, 2014 4:07 pm

Tolo5star wrote:Can you tell me which gpio pins should be connected to Enable A&B
If possible a diagram or a picture would be extremely useful
Thanks in advance
Choose any 2 you are not using from P1 shown at http://elinux.org/RPi_Low-level_peripherals

The ones coloured green are a good choice if you are not sure.

Tolo5star
Posts: 14
Joined: Mon May 05, 2014 8:35 am

Re: Raspberry Pi wifi Keyboard controlled car

Tue May 06, 2014 4:09 pm

Do i have to mention the enabled pins in the coding

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Raspberry Pi wifi Keyboard controlled car

Tue May 06, 2014 5:39 pm

Tolo5star wrote:Do i have to mention the enabled pins in the coding
Yes. Suppose you connect gpio4 to ENA and gpio17 to ENB.

You'll need to set gpios 4 and 17 as outputs and write 1 to them to enable the motors. If you don't writing values to IN1/2/3/4 will have no effect.

Since you are connecting ENA and ENB to gpios (rather than just using jumpers) you can also use those gpios to control the motor speed using PWM.

Apply PWM to gpio4 to control motor A's speed and PWM to gpio17 to control motor B's speed.

Tarcas
Posts: 741
Joined: Thu Jan 09, 2014 5:38 am
Location: USA

Re: Raspberry Pi wifi Keyboard controlled car

Wed May 07, 2014 4:21 am

joan wrote:
Tolo5star wrote:Do i have to mention the enabled pins in the coding
Yes. Suppose you connect gpio4 to ENA and gpio17 to ENB.

You'll need to set gpios 4 and 17 as outputs and write 1 to them to enable the motors. If you don't writing values to IN1/2/3/4 will have no effect.

Since you are connecting ENA and ENB to gpios (rather than just using jumpers) you can also use those gpios to control the motor speed using PWM.

Apply PWM to gpio4 to control motor A's speed and PWM to gpio17 to control motor B's speed.
Caveat: Yes... unless you just connect them to the +5v pins next to them with jumpers.

If you're not using them to PWM, I recommend doing it that way instead. Otherwise, just turn off both IN pins to act the same as turning off the associated Enable pin.

Tolo5star
Posts: 14
Joined: Mon May 05, 2014 8:35 am

Re: Raspberry Pi wifi Keyboard controlled car

Wed May 07, 2014 4:30 am

I am using Jumpers and i am not using pwm

Tarcas
Posts: 741
Joined: Thu Jan 09, 2014 5:38 am
Location: USA

Re: Raspberry Pi wifi Keyboard controlled car

Wed May 07, 2014 4:41 am

Tolo5star wrote:I am using Jumpers and i am not using pwm
Okay. If the jumpers are installed, your code and wiring can both forget that the enable pins even exist. If you only deal with the IN pins, you'll be fine.

Also you can do PWM on the IN pins as well as the Enable ones. The only advantage is that you can separate your code so that the IN pair is ONLY for direction and the Enable is ONLY for speed. It's not much harder to incorporate both functions on the IN pins, so if you add PWM later, that's how I'd do it. Just leave the Enable pins jumpered.

Tolo5star
Posts: 14
Joined: Mon May 05, 2014 8:35 am

Re: Raspberry Pi wifi Keyboard controlled car

Wed May 07, 2014 5:09 am

Tarcas can you explain this to me in detail(The wiring part )i am fairly new in hardware , This is my first pi Project ,This is the code i used :

Code: Select all

#These are the keyboard mappings
#q = Go forward
#a = Stop going forward or back
#z = Go back
#i= Go left
#o = Stop steering
#p = Go right

#Get the GPIO module
import RPi.GPIO as GPIO

#Get the time module
import time

#A routine to control a pair of pins
def ControlAPairOfPins(FirstPin,FirstState,SecondPin,SecondState):
  print "Controlling them pins"
  if FirstState == "1":
         GPIO.output(int(FirstPin),True)
  else:
         GPIO.output(int(FirstPin),False)

  if SecondState == "1":
         GPIO.output(int(SecondPin),True)
  else:
         GPIO.output(int(SecondPin),False)
  #Just retur
  return

####Main body of code

#Get rid of warnings
GPIO.setwarnings(False)

#Set the GPIO mode
GPIO.setmode(GPIO.BOARD)
#Set the pins to be outputs
GPIO.setup(7,GPIO.OUT)
GPIO.setup(16,GPIO.OUT)
GPIO.setup(11,GPIO.OUT)
GPIO.setup(13,GPIO.OUT)
GPIO.setup(19,GPIO.OUT)
GPIO.setup(21,GPIO.OUT)
GPIO.output(7,True)
GPIO.output(16,True)

while True:
  MyChar = raw_input("Press a character:")
  print "You pressed: " + MyChar
  if MyChar == "q":
         ControlAPairOfPins("19","1","21","0")
         ControlAPairOfPins("11","1","13","0")
         print "Forward"
  elif MyChar == "a":
         ControlAPairOfPins("19","0","21","0")
         ControlAPairOfPins("11","0","13","0")
         print "Stop"
  elif MyChar == "z":
         ControlAPairOfPins("19","0","21","1")
         ControlAPairOfPins("11","0","13","1")
         print ("Back")
  elif MyChar == "i":
         ControlAPairOfPins("11","1","13","0")
         print "Left"
  elif MyChar == "o":
         ControlAPairOfPins("11","0","13","0")
         print "Stop steering"
  elif MyChar == "p":
         ControlAPairOfPins("11","0","13","1")
         print "Right"
  else:
         print "Not a command"

Now this works perfectly
Thanks a ton
Last edited by Tolo5star on Fri May 09, 2014 4:47 am, edited 6 times in total.

Tolo5star
Posts: 14
Joined: Mon May 05, 2014 8:35 am

Re: Raspberry Pi wifi Keyboard controlled car

Wed May 07, 2014 5:13 am

here,
gpio16-enb
gpio7-ena
gpio11-in1
gpio13-in2
gpio19-in3
gpio21-in4
gpio6-gnd
For above code
Last edited by Tolo5star on Fri May 09, 2014 4:48 am, edited 2 times in total.

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Raspberry Pi wifi Keyboard controlled car

Wed May 07, 2014 7:10 am

Whenever you post code, Python code in particular, it's best to enclose it in

Code: Select all

 
quotes (highlight the text and press Code above). Space has meaning to Python and it makes other code more readable.

You can edit your own posts.

Have a look at http://elinux.org/RPi_Low-level_peripherals

There are 26 pins on P1. Only 17 are connected to gpios. Therefore only 17 can be switched on or off in software. The other pins are connected to 5V, 3.3V, or ground and are not controllable.

Make sure any code you write only tries to control gpios.

Tolo5star
Posts: 14
Joined: Mon May 05, 2014 8:35 am

Re: Raspberry Pi wifi Keyboard controlled car

Wed May 07, 2014 7:41 am

Sorry this is my first time in forums ,so i have corrected the code so now can you help with the code and wiring ,anyways can you tell me why was the terminal taking and accepting commands but the motors were not responding
also could you give me an example code for enabling


Thanks a ton !!!!!!!!!!

Tolo5star
Posts: 14
Joined: Mon May 05, 2014 8:35 am

Re: Raspberry Pi wifi Keyboard controlled car

Wed May 07, 2014 2:04 pm

Tarcas wrote:
Tolo5star wrote:I am using Jumpers and i am not using pwm
Okay. If the jumpers are installed, your code and wiring can both forget that the enable pins even exist. If you only deal with the IN pins, you'll be fine.

Also you can do PWM on the IN pins as well as the Enable ones. The only advantage is that you can separate your code so that the IN pair is ONLY for direction and the Enable is ONLY for speed. It's not much harder to incorporate both functions on the IN pins, so if you add PWM later, that's how I'd do it. Just leave the Enable pins jumpered.
What does jumpers installed mean

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Raspberry Pi wifi Keyboard controlled car

Wed May 07, 2014 2:07 pm

Tolo5star wrote: ...
What does jumpers installed mean
Have a look at http://www.computerhope.com/jargon/j/jumper.htm

Tolo5star
Posts: 14
Joined: Mon May 05, 2014 8:35 am

Re: Raspberry Pi wifi Keyboard controlled car

Thu May 08, 2014 8:10 am

Anything wrong with the coding ???

BMS Doug
Posts: 3824
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: Raspberry Pi wifi Keyboard controlled car

Thu May 08, 2014 8:42 am

Tolo5star wrote:Anything wrong with the coding ???
I'm probably overthinking it (and I'm no coding expert) but I'd be inclined to do something like this

Code: Select all

# replace In1-4 with GPIO pin assignments
Def motor1forward:
    In1 (true)
    In2 (false)
    
Def motor1backward:
    In1 (false)
    In2 (true)

Def motor1break:
    In1 (true)
    In2 (true)    

Def motor1stop:
    In1 (false)
    In2 (false) 

Def motor2forward:
    In3 (true)
    In4 (false)
    
Def motor1backward:
    In3 (false)
    In4 (true)

Def motor1break:
    In3 (true)
    In4 (true)    

Def motor1stop:
    In3 (false)
    In4 (false)

Def Forward:
    motor1forward
    motor2forward
    
Def ArcRight:
    motor1forward
    motor2stop
    
Def SharpRight:
    motor1forward
    motor2break
    
Def SpinRight:
    motor1forward
    motor2backward
    
Def ArcLeft:
    motor1stop
    motor2forward
    
Def SharpRight:
    motor1break
    motor2forward
    
Def SpinRight:
    motor1backward
    motor2forward
    
Def Reverse:
    motor1 backward
    motor2 backward

# reversing-turns can also be defined.

by defining everything in advance you will be able to test what works for you and what doesn't

this hasn't included PWM yet, that's another issue if you want speed control.
Doug.
Building Management Systems Engineer.

Tolo5star
Posts: 14
Joined: Mon May 05, 2014 8:35 am

Re: Raspberry Pi wifi Keyboard controlled car

Thu May 08, 2014 1:31 pm

BMS then what about the keyboard Commands

BMS Doug
Posts: 3824
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: Raspberry Pi wifi Keyboard controlled car

Thu May 08, 2014 1:42 pm

Once you've proved that each command works as you want it too you can set the keyboard commands to run an action.

while True:
MyChar = raw_input("Press a character:")
print "You pressed: " + MyChar
if MyChar == "q":
Forward
print "Forward"
elif MyChar == "a":
Stop
print "Stop"
elif MyChar == "z":
Backwards
print ("Back")
elif MyChar == "i":
ArcLeft
print "Left"
elif MyChar == "o":
Forward
print "Stop steering"
elif MyChar == "p":
ArcRight
print "Right"
else:
Doug.
Building Management Systems Engineer.

Tolo5star
Posts: 14
Joined: Mon May 05, 2014 8:35 am

Re: Raspberry Pi wifi Keyboard controlled car

Fri May 09, 2014 2:23 am

So can some one sum up all the discussion
and tell me step by step with the gpio connections

Tolo5star
Posts: 14
Joined: Mon May 05, 2014 8:35 am

Re: Raspberry Pi wifi Keyboard controlled car

Fri May 09, 2014 4:38 am

Thanks Everyone my car has been made the modifications to the code have been made

Return to “Automation, sensing and robotics”