EP-0109

From 52Pi Wiki
Revision as of 10:27, 29 September 2019 by Dre (talk | contribs) (→‎How to use)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

DockerPi DIY Programmable intelligent toycar

Description

DockerPi DIY Programmable intelligent toycar is based on DockerPi Motor Board(A) and DockerPi Power Board.You could DIY it's appearance and power.

Of course,if you want to get started directly,we also provide the power board and Arcylic board for you.We offer several packages to you.The DockerPi

Power Board could provide powerful power for the car.You could choose to design its appearance by yourself.Besides,you could connect the camera module

to achieve the function of identifying objects.The following we will show you how to use the keyboard and handle to control the car by pygame.

Remember:Don't assemble and disassemble the board when the power supply is working.They don't support hot plug

Features

  • DockerPi Series
  • Programmable
  • Intelligent toycar
  • Easy to control
  • DIY
  • Selective control
  • Wireless control

Official Compatibility Test

Not only support the following development boards, other development boards can be compatible if they have I2C peripherals. (Note: some software changes may be required)

Platform DockerPi Power Notes
Raspberry Pi All Platform Not Include CM Series & EOL Platform

Gallery

Package Includes

  • Foudation
  * 1 x Battery case
  * 1 x Motor Board
  * 1 x Power Board
  • Addition

Announcements

  • Please turn off the power when you connect it!

  • DockerPi Series Board does not support hot plug
  • Please do not install reverse battery

How to assemble

Configuring I2C(Raspberry Pi)

  • Run sudo raspi-config and follow the prompts to install i2c support for the ARM core and linux kernel
  • Go to Interfacing Options
Raspi-config-1.png

then I2C

Raspi-config-2.png

Enable!

Raspi-config-3.png

Done!

Raspi-config-4.png

How to use

Use the Handle to control the toycar

First you need prepare a TF card to burn the raspbian.You can download the system from the official website.

When you finished this,please connect it by ssh.

  • First,please open the function of wireless function.Execute the following command:
 sudo vim /etc/wpa_supplicant/wpa_supplicant.conf
DockerPi DIY Car(1).png
  • Please configure your wireless
DockerPi DIY Car(2).png
  • Then you need install the python3's module pygame
 sudo apt-get -y install python3-pygame
DockerPi DIY Car(3).png
  • You also need to install python3's smbus
 sudo apt-get -y install python3-smbus
DockerPi DIY Car(4).png
  • Please open the RaspberryPi's I2C
  • You could install the following script to achieve the safeshut down function.
 wget -qO- https://git.io/fj3b9 | sudo bash


Hardware environment


Use the Keyboard to control the toycar


Software environment

First you need prepare a TF card to burn the raspbian.You can download the system from the official website.

When you finished this,please connect it by ssh.

  • First,please open the function of wireless function.Execute the following command:
 sudo vim /etc/wpa_supplicant/wpa_supplicant.conf
DockerPi DIY Car(1).png
  • Please configure your wireless
DockerPi DIY Car(2).png
  • Then you need install the python3's module pygame
 sudo apt-get -y install python3-pygame
DockerPi DIY Car(3).png
  • You also need to install python3's smbus
 sudo apt-get -y install python3-smbus
DockerPi DIY Car(4).png
  • Please open the RaspberryPi's I2C
  • You could install the following script to achieve the safeshut down function.
 wget -qO- https://git.io/fj3b9 | sudo bash


Hardware environment


Codes

  • Create a new file and named:toycar.py and paste following codes:
  • Press the key of W:
  • Press the key of S:
  • Press the key of A:
  • Press the key of D:
import pygame
import sys
import smbus
import time

#MOTOR_DRIVER Address
MOTOR_DRIVER_ADDRESS = 0x18
MOTOR_DRIVER_BUS = 1
bus = smbus.SMBus(MOTOR_DRIVER_BUS)

#MOTOR_DRIVER Functions
MOTOR_DRIVER_SPEED_1_L = 0x01
MOTOR_DRIVER_SPEED_1_H = 0x02
MOTOR_DRIVER_SPEED_2_L = 0x03
MOTOR_DRIVER_SPEED_2_H = 0x04
MOTOR_DRIVER_COUNT_1_L = 0x05
MOTOR_DRIVER_COUNT_1_H = 0x06
MOTOR_DRIVER_COUNT_2_L = 0x07
MOTOR_DRIVER_COUNT_2_H = 0x08
MOTOR_DRIVER_DIRECTION = 0x09
MOTOR_DRIVER_SPEED_NOW_1_L = 0x0a
MOTOR_DRIVER_SPEED_NOW_1_H = 0x0b
MOTOR_DRIVER_SPEED_NOW_2_L = 0x0c
MOTOR_DRIVER_SPEED_NOW_2_H = 0x0d
MOTOR_DRIVER_COUNT_NOW_1_L = 0x0e
MOTOR_DRIVER_COUNT_NOW_1_H = 0x0f
MOTOR_DRIVER_COUNT_NOW_2_L = 0x10
MOTOR_DRIVER_COUNT_NOW_2_H = 0x11
MOTOR_DRIVER_DIRECTION_NOW = 0x12
MOTOR_DRIVER_ENABLE = 0x13
MOTOR_DRIVER_MODE = 0X14
MOTOR_DRIVER_PWM_NOW_2_L = 0x1f
MOTOR_DRIVER_PWM_NOW_2_H = 0x20

pygame.init()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption('pygame event')
def setspeed():
    bus.write_byte_data(MOTOR_DRIVER_ADDRESS,MOTOR_DRIVER_SPEED_1_L,speed & 0xff)  
    bus.write_byte_data(MOTOR_DRIVER_ADDRESS,MOTOR_DRIVER_SPEED_1_H,speed >> 8)  
    bus.write_byte_data(MOTOR_DRIVER_ADDRESS,MOTOR_DRIVER_SPEED_2_L,speed & 0xff)  
    bus.write_byte_data(MOTOR_DRIVER_ADDRESS,MOTOR_DRIVER_SPEED_2_H,speed >> 8)  
    bus.write_byte_data(MOTOR_DRIVER_ADDRESS,MOTOR_DRIVER_ENABLE,1)
def decspeed():
    bus.write_byte_data(MOTOR_DRIVER_ADDRESS,MOTOR_DRIVER_SPEED_1_L,temp1 & 0xff)  
    bus.write_byte_data(MOTOR_DRIVER_ADDRESS,MOTOR_DRIVER_SPEED_1_H,temp1 >> 8)  
    bus.write_byte_data(MOTOR_DRIVER_ADDRESS,MOTOR_DRIVER_SPEED_2_L,temp1 & 0xff)  
    bus.write_byte_data(MOTOR_DRIVER_ADDRESS,MOTOR_DRIVER_SPEED_2_H,temp1 >> 8)    
    bus.write_byte_data(MOTOR_DRIVER_ADDRESS,MOTOR_DRIVER_ENABLE,1)
def turnleft():
    temp0 = 200
    bus.write_byte_data(MOTOR_DRIVER_ADDRESS,MOTOR_DRIVER_SPEED_1_L,temp0 & 0xff)  
    bus.write_byte_data(MOTOR_DRIVER_ADDRESS,MOTOR_DRIVER_SPEED_1_H,((temp0 >> 8) | 0x80))  
    bus.write_byte_data(MOTOR_DRIVER_ADDRESS,MOTOR_DRIVER_SPEED_2_L,(temp0 -100) & 0xff)  
    bus.write_byte_data(MOTOR_DRIVER_ADDRESS,MOTOR_DRIVER_SPEED_2_H,((temp0 -100) >> 8) | 0x80)    
    bus.write_byte_data(MOTOR_DRIVER_ADDRESS,MOTOR_DRIVER_ENABLE,1)
def turnright():
    temp0 = 200
    bus.write_byte_data(MOTOR_DRIVER_ADDRESS,MOTOR_DRIVER_SPEED_1_L,(temp0 - 100) & 0xff)  
    bus.write_byte_data(MOTOR_DRIVER_ADDRESS,MOTOR_DRIVER_SPEED_1_H,((temp0 - 100) >> 8) | 0x80)  
    bus.write_byte_data(MOTOR_DRIVER_ADDRESS,MOTOR_DRIVER_SPEED_2_L,temp0 & 0xff)  
    bus.write_byte_data(MOTOR_DRIVER_ADDRESS,MOTOR_DRIVER_SPEED_2_H,((temp0 >> 8) | 0x80))   
    bus.write_byte_data(MOTOR_DRIVER_ADDRESS,MOTOR_DRIVER_ENABLE,1)
def setdirection(sysnax):
    bus.write_byte_data(MOTOR_DRIVER_ADDRESS,MOTOR_DRIVER_DIRECTION,sysnax)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        #  
        keyboard_list = [pygame.K_w,pygame.K_s,pygame.K_a,pygame.K_d]   
        keys_pressed = pygame.key.get_pressed()    
        if keys_pressed[keyboard_list[0]]:
            print("w")
            speed = 200
            while (speed < 300):
                speed += 10
                setdirection(1)
                setspeed()
                if keys_pressed[keyboard_list[2]]:
                    print("a")
                    turnleft()
                elif keys_pressed[keyboard_list[3]]:
                    print("d")
                    turnright()
        
        elif keys_pressed[keyboard_list[1]]:
            print("s")
            speed = 200
            while (speed < 350):
                speed += 10
                setdirection(2)
                setspeed()
                if keys_pressed[keyboard_list[2]]:
                    print("a")
                    turnleft()
                elif keys_pressed[keyboard_list[3]]:
                    print("d")
                    turnright()
        elif keys_pressed[keyboard_list[2]]:
            print("a")
            turnleft()
            
        elif keys_pressed[keyboard_list[3]]:
            print("d")
            turnright()       
        else:
            temp1 = (bus.read_byte_data(MOTOR_DRIVER_ADDRESS,MOTOR_DRIVER_PWM_NOW_2_H) << 8) | (bus.read_byte_data(MOTOR_DRIVER_ADDRESS,MOTOR_DRIVER_PWM_NOW_2_L))
            print(temp1)
            while (temp1 > 1):
                time.sleep(0.01)
                temp1 -= 4
                decspeed()
            else:
                temp1 = 0
                decspeed()
    pygame.display.update()   
  • Execute it!
 sudo DISPLAY=:0.0 python3 toycar.py
  • Plug the wireless keyboard receiver into the Raspberry Pi,then enjoy it!

FAQ

  • Q: What is the maximum speed the car can reaching?
 A: It will be faster than your imagination,the max is 1300r/min.However, i suggest you don't do like this,it may damage the car.
  • Q: Sometimes,the toycar is out of my control,what's wrong with it?
 A: Maybe the raspberry pie's wireless signal is too weak.Also,this is related to your program.
  • Q: Why i have executed the command:sudo halt,but the Raspberypi and Power Board's Led is still working?
 A: Because you havn't installed the script about power.Please install it then have a try.
  • Q: If i have ran the program and the raspberrypi is fine,i have reed the value of register of speed,but the car has't any response?
 A: Please check the Power Board's power supply pin.It may be broken.
    Meantime,you may not plug the wireless keyboard receiver into the Raspberry Pi

Video

Keywords

DockerPi,DIY,toycar,intelligent,Programmable