Click here to Skip to main content
15,072,051 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello guys , im creating an Raspberry pi controlled Tank , and for that i need an code which are as follows:
(4 wheel drive , controlled through L298N Motor Driver)

(this should be controlled through computer)
when pressed the following keys the following should happen

*press up arrow- the tank goes forward(all 4 wheels go forward)
*press down arrow - the tank goes backwards (all 4 wheels go backwards)
*press left arrow- tank goes left (left 2 wheels go backward, right 2 wheels go forward)
*press right arrow- tank goes right(right 2 wheels go backwards, left 2 wheels go forward)

and additional:
an mouse should be abbled to control an two servos ,for turret system.(like an fpv games)

What I have tried:

Answering to Solution #1 by OriginalGriff:

ah okay thanks for that. reply, anyhow i didnt thought of this much quick resppnse.
okay atleast can u correct this :

//start

# import curses and GPIO
import curses
import RPi.GPIO as GPIO

GPIO.setwarnings(False)

#set GPIO numbering mode and define output pins
GPIO.setmode(GPIO.BOARD)

motor1a = 7
motor1b = 11
motor1e = 22

motor2a = 13
motor2b = 16
motor2e = 15

GPIO.setup(motor1a,GPIO.OUT)
GPIO.setup(motor1b,GPIO.OUT)
GPIO.setup(motor1e,GPIO.OUT)
GPIO.setup(motor2a,GPIO.OUT)
GPIO.setup(motor2b,GPIO.OUT)
GPIO.setup(motor2e,GPIO.OUT)

# Get the curses window, turn off echoing of keyboard to screen, turn on
# instant (no waiting) key response, and use special values for cursor keys
screen = curses.initscr()
curses.noecho()  
curses.cbreak()
curses.halfdelay(3)
screen.keypad(True)

try:
        while True:   
            char = screen.getch()
            if char == ord('q'):
                break
            elif char == curses.KEY_UP:
                GPIO.output(motor1a,GPIO.HIGH)
                GPIO.output(motor1b,GPIO.LOW)
                GPIO.output(motor1e,GPIO.HIGH)
                GPIO.output(motor2a,GPIO.HIGH)
  GPIO.output(motor2b,GPIO.LOW)
  GPIO.output(motor2e,GPIO.HIGH)
            elif char == curses.KEY_DOWN:
                GPIO.output(motor1a,GPIO.LOW)
                GPIO.output(motor1b,GPIO.HIGH)
                GPIO.output(motor1e,GPIO.HIGH)
                GPIO.output(motor2a,GPIO.LOW)
  GPIO.output(motor2b,GPIO.HIGH)
  GPIO.output(motor2e,GPIO.HIGH)
            elif char == curses.KEY_RIGHT:
                GPIO.output(motor1a,GPIO.HIGH)
                GPIO.output(motor1b,GPIO.LOW)
                GPIO.output(motor1e,GPIO.HIGH)
                GPIO.output(motor2a,GPIO.LOW)
  GPIO.output(motor2b,GPIO.HIGH)
  GPIO.output(motor2e,GPIO.HIGH)
            elif char == curses.KEY_LEFT:
                GPIO.output(motor1a,GPIO.LOW)
                GPIO.output(motor1b,GPIO.HIGH)
                GPIO.output(motor1e,GPIO.HIGH)
                GPIO.output(motor2a,GPIO.HIGH)
  GPIO.output(motor2b,GPIO.LOW)
  GPIO.output(motor2e,GPIO.HIGH)
            elif char == 10:
                GPIO.output(motor1a,GPIO.LOW)
                GPIO.output(motor1b,GPIO.LOW)
                GPIO.output(motor1e,GPIO.LOW)
                GPIO.output(motor2a,GPIO.LOW)
  GPIO.output(motor2b,GPIO.LOW)
  GPIO.output(motor2e,GPIO.LOW)
             
finally:
    #Close down curses properly, inc turn echo back on!
    curses.nocbreak(); screen.keypad(0); curses.echo()
    curses.endwin()
    GPIO.cleanup()


// end
in the above code , the motor keeps running until next key is pressed , so how to make it to work like "press key to run , and stop when the key is relesed"
Posted
Updated 23-Sep-18 8:37am
v2

Quote:
for that i need an code which are as follows:

It doesn't quite work like that.
We do not do your work for you.
If you want someone to write your code, you have to pay - I suggest you go to Freelancer.com and ask there.

But be aware: you get what you pay for. Pay peanuts, get monkeys.

The idea of "development" is as the word suggests: "The systematic use of scientific and technical knowledge to meet specific objectives or requirements." BusinessDictionary.com[^]
That's not the same thing as "have a quick google and give up if I can't find exactly the right code".
So either pay someone to do it, or learn how to write it yourself. We aren't here to do it for you.
   
Comments
Nelek 23-Sep-18 14:37pm
   
Hey OG... the OP just posted a non solution to give more information and show his code. I moved it above. Please have a look
yeah okay thanks for that. reply, anyhow i didnt thought of this much quick resppnse.
okay atleast can u correct this :

//start

# import curses and GPIO
import curses
import RPi.GPIO as GPIO

GPIO.setwarnings(False)

#set GPIO numbering mode and define output pins
GPIO.setmode(GPIO.BOARD)

motor1a = 7
motor1b = 11
motor1e = 22

motor2a = 13
motor2b = 16
motor2e = 15

GPIO.setup(motor1a,GPIO.OUT)
GPIO.setup(motor1b,GPIO.OUT)
GPIO.setup(motor1e,GPIO.OUT)
GPIO.setup(motor2a,GPIO.OUT)
GPIO.setup(motor2b,GPIO.OUT)
GPIO.setup(motor2e,GPIO.OUT)

# Get the curses window, turn off echoing of keyboard to screen, turn on
# instant (no waiting) key response, and use special values for cursor keys
screen = curses.initscr()
curses.noecho()  
curses.cbreak()
curses.halfdelay(3)
screen.keypad(True)

try:
        while True:   
            char = screen.getch()
            if char == ord('q'):
                break
            elif char == curses.KEY_UP:
                GPIO.output(motor1a,GPIO.HIGH)
                GPIO.output(motor1b,GPIO.LOW)
                GPIO.output(motor1e,GPIO.HIGH)
                GPIO.output(motor2a,GPIO.HIGH)
  GPIO.output(motor2b,GPIO.LOW)
  GPIO.output(motor2e,GPIO.HIGH)
            elif char == curses.KEY_DOWN:
                GPIO.output(motor1a,GPIO.LOW)
                GPIO.output(motor1b,GPIO.HIGH)
                GPIO.output(motor1e,GPIO.HIGH)
                GPIO.output(motor2a,GPIO.LOW)
  GPIO.output(motor2b,GPIO.HIGH)
  GPIO.output(motor2e,GPIO.HIGH)
            elif char == curses.KEY_RIGHT:
                GPIO.output(motor1a,GPIO.HIGH)
                GPIO.output(motor1b,GPIO.LOW)
                GPIO.output(motor1e,GPIO.HIGH)
                GPIO.output(motor2a,GPIO.LOW)
  GPIO.output(motor2b,GPIO.HIGH)
  GPIO.output(motor2e,GPIO.HIGH)
            elif char == curses.KEY_LEFT:
                GPIO.output(motor1a,GPIO.LOW)
                GPIO.output(motor1b,GPIO.HIGH)
                GPIO.output(motor1e,GPIO.HIGH)
                GPIO.output(motor2a,GPIO.HIGH)
  GPIO.output(motor2b,GPIO.LOW)
  GPIO.output(motor2e,GPIO.HIGH)
            elif char == 10:
                GPIO.output(motor1a,GPIO.LOW)
                GPIO.output(motor1b,GPIO.LOW)
                GPIO.output(motor1e,GPIO.LOW)
                GPIO.output(motor2a,GPIO.LOW)
  GPIO.output(motor2b,GPIO.LOW)
  GPIO.output(motor2e,GPIO.LOW)
             
finally:
    #Close down curses properly, inc turn echo back on!
    curses.nocbreak(); screen.keypad(0); curses.echo()
    curses.endwin()
    GPIO.cleanup()


// end
in the above code , the motor keeps running until next key is pressed , so how to make it to work like "press key to run , and stop when the key is relesed"
   
Comments
Nelek 23-Sep-18 14:36pm
   
Please don't use the solutions to speak as it would be a chat. They won't remain chronologically, so following the "conversation" will be a mess. Instead you might use the "improve question" widget to add relevant information in there, and then use a "have a question or comment?" (as I am doing now with you), to inform that you posted new information to the one requesting it.

I am movign this code to the question and saying OriginalGriff to have a look the way you should do it in the future.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900