Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
raspberry pi tank gpio help
#1
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()
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Picture changing triggered by GPIO q_nerk 2 153 Dec-14-2020, 03:32 PM
Last Post: DeaD_EyE
  GPIO high if network IP has good ping duckredbeard 3 256 Oct-12-2020, 10:41 PM
Last Post: bowlofred
  Where should I place GPIO.cleanup() shallanq 2 485 Apr-11-2020, 05:02 AM
Last Post: shallanq
  Orange PI Win steering LEDs connect to GPIO djmcg 0 372 Dec-27-2019, 08:26 AM
Last Post: djmcg
  GPIO time in HIGH LOW boris_za 1 517 Dec-07-2019, 01:48 PM
Last Post: Larz60+
  RPi GPIO Flashing LED nickayres 1 897 Jun-07-2019, 01:32 PM
Last Post: gontajones
  GPIO output timing help needed skid 5 1,649 Jan-23-2018, 04:12 PM
Last Post: skid
  Setting GPIO on Raspberry PI using a function jmjasper1 6 2,283 Dec-29-2017, 08:42 PM
Last Post: nilamo
  Use kivy app to call a php website to change the state of a gpio pin on rasperbery pi Battillo 1 1,567 Jun-05-2017, 10:33 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020