How to do it...

The unit will need to be wired as follows (this will be similar for other H-bridge type controllers, but check with the relevant datasheet if unsure).

The following table shows how the motors and motor power supply connect to the H-bridge controller module:

The motor side of the module – connecting to the battery and motors

Motor A

VMS

GND

5V OUT

Motor B

Left motor red wire

Left motor black wire

Battery positive

Battery GND

None

Right motor red wire

Right motor black wire

 

The following table shows how the H-bridge controller module connects to the Raspberry Pi:

Control side of the module – connecting to the Raspberry Pi GPIO header

ENA

IN1

IN2

IN3

IN4

ENB

GND

5V

None

Pin 15

Pin 16

Pin 18

Pin 22

None

Pin 6

None

 

It is recommended that you keep the pull-up resistor jumpers on (UR1-UR4) and allow the motor supply to power the onboard voltage regulator, which will in turn power the L298N controller (jumper 5V_EN). The onboard regulator (the 78M05 device) can supply up to 500 mA, enough for the L298N controller plus any additional circuits, such as an I/O expander (see the There's more... section for more information). Both the ENA and ENB pins should be disconnected (the motor output will stay enabled by default).

You will need to make the following changes to the previous rover_drivefwd.py script (you can save it as rover_drive.py).

At the top of the file, redefine PINS, as follows:

PINS=[15,16,18,22]   # PINS=[L_FWD,L_BWD,R_FWD,R_BWD] 

And update the control patterns, as follows:

FWD=[ON,OFF,ON,OFF] 
BWD=[OFF,ON,OFF,ON] 
RIGHT=[OFF,ON,ON,OFF] 
LEFT=[ON,OFF,OFF,ON] 

Next, we need to add the backwards command to cmd(), as follows:

  def cmd(self,char,step=STEP): 
    if char == 'f': 
      self.drive(FWD,step) 
    elif char == 'b': 
      self.drive(BWD,step) 
    elif char == 'r': 
      self.drive(RIGHT,step) 
    elif char == 'l': 
      self.drive(LEFT,step) 
    elif char == '#': 
      time.sleep(step) 

Finally, we can update the prompt that we have within the main() function to include b (backwards) as an option, as follows:

print("Enter CMDs [f,b,r,l,#]:") 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.117.214