2

initially my question was about how to fix my code. You informed me that usage of that python class will not work with my setup of one motor. I agree and I want to instead use the GPIO library to control the board controlling the motor. the example code I found:
enter image description here

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)
while True:
   if ( GPIO.input(23) == False ):
      print(‘do something’)

I do not know how to edit the code to work with my project.] but I am using IN1 on the controller board and GPIO2 from the pi.

I answered this question but it's not an answer, it is more of a notice that I am going to use different parts. Many of the answers here pointed out that I should try the tutorials and/or use different parts so I will. thanks to everyone who helped!!

  • 2
    which part of the code do you not understand? – jsotola May 13 '19 at 01:51
  • 1
    You are trying to run before you can walk. You need to understand some basics before attempting this task. I suggest you work through 1) some Python tutorials and 2) look at https://projects.raspberrypi.org/en/projects/?interests[]=robotics – joan May 13 '19 at 07:22
  • I am not trying to skip steps, I will gladly do tutorials, thanks! – thecanmanisback May 13 '19 at 23:59

4 Answers4

1

I am just going to use different parts. These parts are not usable or are too hard to use at this point so I am going to use different motors and stick with GPIOzero

0

The line robot = Robot(left = (2, 2), right = (2, 2)) is saying you are controlling the left motor with GPIO 2 and 2 and the right motor with GPIO 2 and 2. This is illegal.

You need to specify four different GPIO. Two GPIO are needed to control each motor as follows:

GPIO#1 GPIO#2  motor
0      0       stop
0      1       clockwise
1      0       counterclockwise
1      1       brake

See https://gpiozero.readthedocs.io/en/stable/api_boards.html#robot

joan
  • 67,803
  • 5
  • 67
  • 102
  • It is only 1 motor. The project is a pi with GPIO2 connected to an H-Bridge which is connected to 1 motor on a car. should that line be `robot = Robot(left = (2, 1), right = (2, 1))`? – thecanmanisback May 12 '19 at 22:07
  • Frankly it sounds wrong. The robot class expects a left and right motor being driven by a motor driver board. If you are not doing that you should not be using the robot class. – joan May 12 '19 at 22:12
  • oh no. 1. I though the H-Bridge acted as the motor driver board... is that not true? 2.The error was about the way I defined 'robot' so are you saying that the error is happening because I have 1 motor instead of 4? 3. What would you use instead of this class if it was your project? Would you use shell commands or just a different python class? – thecanmanisback May 12 '19 at 22:57
  • 1
    @thecanmanisback This is the wrong site to ask open ended questions. You would be better off asking on raspberrypi.org/forums. – joan May 12 '19 at 23:00
  • I edited my question at the top to fit more into what this forums is used for. @joan – thecanmanisback May 12 '19 at 23:40
0

I suggest you return to using gpiozero, but select a suitable class.

We don't know anything about your robot (include detail in your question).

I guess it has a single motor with forward/reverse so the following would be appropriate:-

https://gpiozero.readthedocs.io/en/v1.2.0/api_output.html#motor

Milliways
  • 54,718
  • 26
  • 92
  • 182
  • I made a diagram of my project that includes a lot of detail: https://snag.gy/7dL0pb.jpg it helps me to have a very simple working program that I can reverse engineer and learn from. Here is the code at the link you gave: `from gpiozero import Motor` `motor = Motor(17, 18)` `motor.forward()` In my case would I change the code to `motor = Motor(1, 2)` and just ignore the 2 and have no reverse in this project? (no reverse is fine here) – thecanmanisback May 13 '19 at 01:31
  • it is hard to see in the diagram, but the raspberry pi is plugged into the IN1 port on the H-Bridge – thecanmanisback May 13 '19 at 01:44
  • Paste the image into your question not a link DO NOT put details in Comments – Milliways May 13 '19 at 01:54
  • I pasted the image into my question, and I am going to return to using GPIOzero but I am also going to get a 2 motor chassis that uses the motors recommended in the pi tutorials and just abandon my homemade model car with one motor for now. – thecanmanisback May 14 '19 at 00:39
  • @thecanmanisback You should try to get your initial hardware going; abandoning a project is not the way to learn. People are willing to help, BUT you need to post specifications of the parts you are using NOT pictures. You NEED 2 connections to the Pi to use a H bridge (as well as a Gnd connection). This will apply to any new hardware. – Milliways May 14 '19 at 00:58
  • the pi needs a ground connection? – thecanmanisback May 15 '19 at 00:41
  • I did not know I needed two connections to the pi to use an H-Bridge. That is a useful fact to know! That is probably exactly why my project did not work, lol It is a weird meccano motor that the company did not have any information about when I emailed them, my H-Bridge is L298N. I am going to come back to this project once I have made some similar, simpler projects. – thecanmanisback May 15 '19 at 00:47
0

Question

Usage of that python class will not work with my setup of one motor. I agree and I want to instead use the GPIO library to control the board controlling the motor.

Example code I found: import RPi.GPIO as GPIO

I do not know how to edit the code to work with my project.] but I am using IN1 on the controller board and GPIO2 from the pi.

Answer

  1. It is not at all clear what DC motor driver you are using. Is it L293D, L298N? You might like to compare you motor driver board the L298N used by the following tutorial.

    Build a robot buggy - projects.raspberrypi.org

build a robot buggy

  1. It is also not clear if you are use Rpi.GPIO module or gpiozero1.5. Gpiozero assumes a generic motor driver board. If your motor is not that generic, you might find teething problem. If you are using gpioZero, you should start with "from gpiozero import Robot".

  2. gpiozero's Robot is a bit complicated. For example, it has a "curve" thing:

curve_left (float) – The amount to curve left while moving backwards, by driving the left motor at a slower speed. Maximum curve_left is 1, the default is 0 (no curve). This parameter can only be specified as a keyword parameter, and is mutually exclusive with curve_right.

The curve function to drive two motors at different speeds is for sure not for newbies. I would recommend to start with Build a robot buggy - projects.raspberrypi.org, which takes you by the hand, starts with simple movements, and finally hints you how to drive you robot along a square path.

But if you have already begun and got stuck with GpioZeroRobot, I would suggest you to remove all advanced stuff like class "curve" and "phase/enable". They are too advanced for newbies.

References

Build a robot buggy - projects.raspberrypi.org

Why isn’t my Raspberry Pi motor spinning?

Why don't my motors rotate?

Gpio Zero Robot 16.1.15. - GPIO Zero

A generic dual-motor robot

This class is constructed with two tuples representing the forward and backward pins of the left and right controllers respectively. For example, if the left motor’s controller is connected to GPIOs 4 and 14, while the right motor’s controller is connected to GPIOs 17 and 18.

Example to drive the robot

from gpiozero import Robot
robot = Robot(left=(4, 14), right=(17, 18))
robot.forward()

curve_left (float)

The amount to curve left while moving backwards, by driving the left motor at a slower speed. Maximum curve_left is 1, the default is 0 (no curve). This parameter can only be specified as a keyword parameter, and is mutually exclusive with curve_right.

Phase/Enable motor board

class gpiozero.PhaseEnableRobot(left, right, *, pwm=True, pin_factory=None)

Extends CompositeDevice to represent a dual-motor robot based around a Phase/Enable motor board.

tlfong01
  • 4,384
  • 3
  • 9
  • 23
  • I am going to stop using my homemade car with one motor for now, and buy a 2-wheel buggy that uses the recommended motors from the raspberry pi tutorials. – thecanmanisback May 14 '19 at 00:37
  • Can also find tutorials using L298N motor board. I confess that I actually do not understand the gpiozero code. EG, they use "tuple" which I have never used, even having played python a couple of years. I do use "list" but for simple things like a list of 4 motors. I heard that "tuple" is not changeable, very efficient etc etc. I only use integers, not floating they are using! Tell you one more little secret - I don't quite understand what actually is a "class". I only use "functions", never "class". I know "functions" can easily be converted to "class", but not vice versa. – tlfong01 May 14 '19 at 01:38
  • yea I don't use tuple either, I always use lists. I do however understand class, the best way I can think to quickly explain it is that it is a type of object. You can make objects or instances of a class. For example you are of class human, and you are object/instance tlfong01 of class human. Anyway, thanks for the help, but I am going to buy different motors for this project because the problems I was having all originated from the motor I have! – thecanmanisback May 15 '19 at 00:52
  • @ thecanmanisback Thank you for your help explaining the OO thing. I agree with almost all you said, except the phrase that confuses me - "It [a class] is a type of object". Anyway, I will google more to clear my mind. And for motors, I highly recommend the very newbie little yellow motor. – tlfong01 May 15 '19 at 01:30