Learining project

So I have cobbled together a bunch of parts, I got a chassis with motors and encoders, I’m only using the chassis and motors though, and I got pi hats from adafruit a motor one and a servo one. I’m accumulating different things and playing around to see what I can do with them. So far coding is the think I am having the most trouble with, but I thought I would post my code that I’ve cobbled together having no real coding experience. I’ve got some continuous rotation servos on the way to play with too, so might start cobbling together a little arm or something.

An observation I’d make is that coders speak another language, and don’t seem to be able to explain in English how to transfer conceptual thinking in to coding. So there’s a massive gap between for example the adafruit test code, to usable this is how you make motors do these things.

These are literally my first two semi functioning attempts at python, so yeah, if you have any pointers or where I can go next, feel free.

This is my rover test code, it gives me a preview of what the rover is seeing and I can drive it around the house. Not sure if curses is the best option so I’m currently looking at pygame as an alternative. Also I really want to make a proper gui, maybe with clickable rover controls. I’d like the gui to utilise both a pi camera and a USB camera, so that I can have a driving cam on the chassis, and another cam on a pan tilt servo setup.

import time
import board
import os
import curses
from adafruit_motorkit import MotorKit
from picamera import PiCamera

stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
stdscr.keypad(True)

camera = PiCamera()
camera.rotation = 0
camera.resolution = (640, 480)
camera.framerate = 60

kit = MotorKit(i2c=board.I2C())

class rover:
    def stop():
        kit.motor1.throttle = 0
        kit.motor2.throttle = 0
        kit.motor3.throttle = 0
        kit.motor4.throttle = 0
    def coast_stop():
        kit.motor1.throttle = None
        kit.motor2.throttle = None
        kit.motor3.throttle = None
        kit.motor4.throttle = None
    def fwd():
        kit.motor1.throttle = 0.5
        kit.motor2.throttle = 0.5
        kit.motor3.throttle = 0.5
        kit.motor4.throttle = 0.5    
    def ffwd():
        kit.motor1.throttle = 1
        kit.motor2.throttle = 1
        kit.motor3.throttle = 1
        kit.motor4.throttle = 1    
    def back():
        kit.motor1.throttle = -0.5
        kit.motor2.throttle = -0.5
        kit.motor3.throttle = -0.5
        kit.motor4.throttle = -0.5
    def spinL():
        kit.motor1.throttle = -0.5
        kit.motor2.throttle = 0.5
        kit.motor3.throttle = -0.5
        kit.motor4.throttle = 0.5
    def spinR():
        kit.motor1.throttle = 0.5
        kit.motor2.throttle = -0.5
        kit.motor3.throttle = 0.5
        kit.motor4.throttle = -0.5


while True:
    camera.start_preview(fullscreen=False, window = (100, 20, 640, 480)) 
    char = stdscr.getch()
    if char == ord('q'):
        #if q is pressed quit
        break
    if char == curses.KEY_BACKSPACE:
        rover.stop()
    if char == curses.KEY_UP:
        rover.fwd()
    if char == curses.KEY_DOWN:
        rover.back()
    if char == curses.KEY_LEFT:
        rover.spinL()
    iwhf char == curses.KEY_RIGHT:
        rover.spinR()

rover.stop()
camera.stop_preview()
camera.close()
curses.nocbreak()
stdscr.keypad(False)
curses.echo()
curses.endwin()

Another thing I jacked up yesterday after having the idea circulating in my head but no idea how to make the code work was keyboard control for a pan and tilt setup. I like this one because it centres it at the start, and on exit. A better method would allow for movement in either axis at the same time, but I will get there.

import curses
import os
from adafruit_servokit import ServoKit

kit = ServoKit(channels=16)

stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
stdscr.keypad(True)

kit.servo[0].angle = 90
kit.servo[1].angle = 90

class thingo:
    def centre():
        kit.servo[0].angle = 90
        kit.servo[1].angle = 90
    def up():
        if kit.servo[1].angle <= 175:
            kit.servo[1].angle = (kit.servo[1].angle)+5
    def down():
        if kit.servo[1].angle >= 5:
            kit.servo[1].angle = (kit.servo[1].angle)-5
    def left():
        if kit.servo[0].angle <= 175:
            kit.servo[0].angle = (kit.servo[0].angle)+5
    def right():
        if kit.servo[0].angle >= 5:
            kit.servo[0].angle = (kit.servo[0].angle)-5

while True:
    char = stdscr.getch()
    if char == ord('q'):
        #if q is pressed quit
        break
    if char == curses.KEY_BACKSPACE:
        thingo.centre()
    if char == curses.KEY_UP:
        thingo.up()
    if char == curses.KEY_DOWN:
        thingo.down()
    if char == curses.KEY_LEFT:
        thingo.left()
    if char == curses.KEY_RIGHT:
        thingo.right()

kit.servo[0].angle = 90
kit.servo[1].angle = 90
curses.nocbreak()
stdscr.keypad(False)
curses.echo()
curses.endwin()
2 Likes

Hey Bridget,

Looks good so far!

One thing that help me if I’m ever stuck with a bit of code is a flowchart on how I want it to work. For a robot this can be quite hard - a state machine might be useful here, rather than some set program that linearly moves through the functions. Here’s a link to the Wiki page: Finite-state machine - Wikipedia

As for someplace to learn, I would definitely check out Core’s guide on Python: https://core-electronics.com.au/tutorials/python-workshop.html

I havent delved into GUI’s too much to cant chime in there, maybe some other members of the community could help :smiley:
Liam.

2 Likes