Python Forum

Full Version: raspberry pi tank gpio help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i recently purchased the yahboom g1 tank kit and a raspberry pi 4b, i managed to figure out enough python to get it to move but im not understanding sockets and how to use it to send data so i can control the tank through ipv4. this is my first python project, and the end goal is to have the mjpeg streaming utility installed stream video to python somehow using pygame and using the keypress events in pygame move the tank around and change the speed. im just not sure how to do anything yet other than get it to move using keypress events and pygame. can someone point me to some resources to explain how i would accomplish this? it seems like if i can figure out how to get this to work i wold learn a decent amount of python in the process. also the hope was to figure it out and add a robotic arm to go grocery shopping for me, and then make the code open source. i think wireless payment is probably asking too much, i think if i put a wireless card on the arm and something to move it with a stylus and hit credit i should be able to make shopping from home possible without worrying about getting infected or getting anyone on instacart infected with, u know what( not trying to stir stuff up)

anyone that can help in this matter? i figured this would be the perfect time to learn to code, im not doing much else now anyway Big Grin

p.s. yes i know the upper shelves will probably be off limit with such a small machine but my store actually has the things i need on the bottom shelves. like i thought this out way too much already Think
import pygame
#-*- coding:UTF-8 -*-
import RPi.GPIO as GPIO
import time
#define carspeedcontrol to 50%
CarSpeedControl = 50
#Definition of  motor pin 
IN1 = 20
IN2 = 21
IN3 = 19
IN4 = 26
ENA = 16
ENB = 13

GPIO.setwarnings(False)

#reset pwm speed to 50
def pwmrst():
    pwm_ENA.ChangeDutyCycle(50)
    pwm_ENB.ChangeDutyCycle(50)
    CarSpeedControl = 50

#setting motor pins
def motor_init():
    global pwm_ENA
    global pwm_ENB
    GPIO.setup(ENA,GPIO.OUT,initial=GPIO.HIGH)
    GPIO.setup(IN1,GPIO.OUT,initial=GPIO.LOW)
    GPIO.setup(IN2,GPIO.OUT,initial=GPIO.LOW)
    GPIO.setup(ENB,GPIO.OUT,initial=GPIO.HIGH)
    GPIO.setup(IN3,GPIO.OUT,initial=GPIO.LOW)
    GPIO.setup(IN4,GPIO.OUT,initial=GPIO.LOW)
    #Set the PWM pin and frequency is 2000hz
    pwm_ENA = GPIO.PWM(ENA, 2000)
    pwm_ENB = GPIO.PWM(ENB, 2000)


#forward
def run():
    GPIO.output(IN1, GPIO.HIGH)
    GPIO.output(IN2, GPIO.LOW)
    GPIO.output(IN3, GPIO.HIGH)
    GPIO.output(IN4, GPIO.LOW)
    #PWM duty cycle is set to 100(0--100)
    pwm_ENA.start(50)
    pwm_ENB.start(50)
    pwm_ENA.ChangeDutyCycle(CarSpeedControl)
    pwm_ENB.ChangeDutyCycle(CarSpeedControl)
    
    #back
def back():
    GPIO.output(IN1, GPIO.LOW)
    GPIO.output(IN2, GPIO.HIGH)
    GPIO.output(IN3, GPIO.LOW)
    GPIO.output(IN4, GPIO.HIGH)
    pwm_ENA.ChangeDutyCycle(CarSpeedControl)
    pwm_ENB.ChangeDutyCycle(CarSpeedControl)

#turn left
def left():
    GPIO.output(IN1, GPIO.LOW)
    GPIO.output(IN2, GPIO.LOW)
    GPIO.output(IN3, GPIO.HIGH)
    GPIO.output(IN4, GPIO.LOW)
    pwm_ENA.ChangeDutyCycle(CarSpeedControl)
    pwm_ENB.ChangeDutyCycle(CarSpeedControl)
    
    #turn right
def right():
    GPIO.output(IN1, GPIO.HIGH)
    GPIO.output(IN2, GPIO.LOW)
    GPIO.output(IN3, GPIO.LOW)
    GPIO.output(IN4, GPIO.LOW)
    pwm_ENA.ChangeDutyCycle(CarSpeedControl)
    pwm_ENB.ChangeDutyCycle(CarSpeedControl)


#turn right in place
def spin_right():
    GPIO.output(IN1, GPIO.HIGH)
    GPIO.output(IN2, GPIO.LOW)
    GPIO.output(IN3, GPIO.LOW)
    GPIO.output(IN4, GPIO.HIGH)
    pwm_ENA.ChangeDutyCycle(CarSpeedControl)
    pwm_ENB.ChangeDutyCycle(CarSpeedControl)
    
#turn left in place
def spin_left():
    GPIO.output(IN1, GPIO.LOW)
    GPIO.output(IN2, GPIO.HIGH)
    GPIO.output(IN3, GPIO.HIGH)
    GPIO.output(IN4, GPIO.LOW)
    pwm_ENA.ChangeDutyCycle(CarSpeedControl)
    pwm_ENB.ChangeDutyCycle(CarSpeedControl)

def w4c():
    time.sleep(0.5)
    GPIO.output(IN1, GPIO.LOW)
    GPIO.output(IN2, GPIO.LOW)
    GPIO.output(IN3, GPIO.LOW)
    GPIO.output(IN4, GPIO.LOW)
    time.sleep(0.01)
    
    #brake
def brake():
   GPIO.output(IN1, GPIO.LOW)
   GPIO.output(IN2, GPIO.LOW)
   GPIO.output(IN3, GPIO.LOW)
   GPIO.output(IN4, GPIO.LOW)

#Set the GPIO port to BCM encoding mode.
GPIO.setmode(GPIO.BCM)
#initialize pygame
pygame.init()
 
#make screen
screen = pygame.display.set_mode((800, 600))
motor_init()
#variable for when running
running = True
def main_pro():

    #loop that continues as long as running is true
    running = True
    while running == True:
        for event in pygame.event.get():
            kpress=pygame.key.get_pressed()
            if event.type == pygame.QUIT:
                running = False
                pygame.quit()
                print("quitting suceeded")
            elif kpress[pygame.K_UP]:
                print("forward ho")
                run()
                w4c()
            elif kpress[pygame.K_DOWN]:
                print("reverse engines!")
                back()
                w4c()
            elif kpress[pygame.K_LEFT]:
                print("steer left!")
                spin_left()
                w4c()
            elif kpress[pygame.K_RIGHT]:
                print("steer right!!")
                spin_right()
                w4c()
            elif kpress[pygame.K_SPACE]:
                print("halt engines!")
                brake()
                
          
          


CarSpeedControl = 50
main_pro()
I just made this same post on another thread:
I can't vouch for this tutorial, but the authors have a solid reputation, so expect it to be complete and useful.
https://realpython.com/python-sockets/
try the first couple of lessons and make your own assessment.