Salted fish ZTMR example-remote control trolley

Salted fish ZTMR example-remote control trolley


* It feels so easy to control the LED. Now let's control the motor and make a remote control car (simple). Just replace the previous LED with a motor function. Get started ~~~~~ *
Main control board: ZTMR development board

PS2 handle
Insert picture description here
N20 reduction motor ( motor on four-wheel drive vehicle)
Insert picture description here

PS2 handle digital Representative button Car status
5 on go (go straight)
6 right right
7 under back
8 left left
0 Defaults stopdj (stop)

Routine: The prototype of the remote control car walks up
main.py

# main.py -- put your code here!
import ps2
import pyb
from car import Car

while True:
    pyb.delay(300)
    print(ps2.ps2_key()) 
    if (ps2.ps2_key()==5):
        Car.go(40)        
    elif (ps2.ps2_key()==6):
        Car.right(40)
    elif (ps2.ps2_key()==7):
        Car.back(40)
    elif (ps2.ps2_key()==8):
        Car.left(40)
    else:
        Car.stopdj()

ps2.py

from machine import Pin
import time
def ps2_test():
  print('test ok')
di=Pin(Pin.cpu.C0,Pin.IN,Pin.PULL_DOWN)                     
do=Pin(Pin.cpu.C1,Pin.OUT, )                                             
cs=Pin(Pin.cpu.C2,Pin.OUT,)     #PULL_UP                                          
clk=Pin(Pin.cpu.C3,Pin.OUT,)   
#D0 16  dat=DI
#D2 04  cmd=DO
#D3  0  CS =CS
#D4  02  CLK=CLK                                                      
def DO_H():
  do.value(1)
def DO_L():
  do.value(0)
def CS_H ():
  cs.value(1)
def CS_L():
  cs.value(0)
def CLK_H ():
  clk.value(1)
def CLK_L():
  clk.value(0)

#常量按钮
PSB_SELECT     = 1
PSB_L3         = 2
PSB_R3         = 3
PSB_START      = 4
PSB_PAD_UP     = 5
PSB_PAD_RIGHT  = 6
PSB_PAD_DOWN   = 7
PSB_PAD_LEFT   = 8
PSB_L2         = 9
PSB_R2         = 10
PSB_L1         = 11
PSB_R1         = 12
PSB_GREEN      = 13
PSB_RED        = 14
PSB_BLUE       = 15
PSB_PINK       = 16
PSB_TRIANGLE   = 13
PSB_CIRCLE     = 14
PSB_CROSS      = 15
PSB_SQUARE     = 26
# 左摇杆
PSS_RX = 5                
PSS_RY = 6
PSS_LX = 7
PSS_LY = 8
mask=[
PSB_SELECT,
    PSB_L3,
    PSB_R3 ,
    PSB_START,
    PSB_PAD_UP,
    PSB_PAD_RIGHT,
    PSB_PAD_DOWN,
    PSB_PAD_LEFT,
    PSB_L2,
    PSB_R2,
    PSB_L1,
    PSB_R1 ,
    PSB_GREEN,
    PSB_RED,
    PSB_BLUE,
    PSB_PINK]
comd=[0x01,0x42]
data=[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00]
def ps2_init():
  CLK_H()
  DO_H()
  time.sleep_ms(10)
def ps2_cmd(cmd):
  global data
  data[1]=0
  for ref in (1,2,4,8,16,32,64,128):
    if ( ref & cmd):
     DO_H()
    else:
     DO_L()
    CLK_H ()
    time.sleep_us(50)
    CLK_L()
    time.sleep_us(50)
    CLK_H ()
    if(di.value()==1):
     data[1]=ref|data[1]
def ps2_red():
  global data
  global comd
  CS_L()
  ps2_cmd(comd[0])
  ps2_cmd(comd[1])
  CS_H()
  if(data[1]==57):
    return 0#red light
  else:
    return 1#not red
def ps2_read():
  global data
  global comd
  byte=0
  ref=0x01
  CS_L()
  ps2_cmd(comd[0])
  ps2_cmd(comd[1])
  for byte in (2,3,4,5,6,7,8):
    for ref in (1,2,4,8,16,32,64,128):
     CLK_H ()
     CLK_L()
     time.sleep_us(50)
     CLK_H ()
     if(di.value()==1):
      data[byte]= ref|data[byte]
    time.sleep_us(50)
  CS_H ()
def ps2_clear():#ok
  global data
  for i in range(0,9,1):
    data[i]=0

def ps2_andata(button):
  global data
  return data[button]
def ps2_key():
  global data
  global mask
  ps2_clear()
  ps2_read()
  handkey=(data[4]<<8)|data[3]
  for index in (0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15):
    if (( handkey&(1<<(mask[index]-1)))==0):
     return index+2
  return 0

car.py

# main.py -- put your code here!
from pyb import Pin, Timer,delay
from time import sleep_us,ticks_us,sleep
cs = Pin('B10',Pin.OUT_PP)    #B10设置为输出引脚输出高电平
cs(1)
ch1 =None
ch2 =None     #初始化
AI1 = Pin('B12',Pin.OUT_PP)    #右侧马达
AI2 = Pin('B13',Pin.OUT_PP)
BI1 = Pin('B14',Pin.OUT_PP)    #左侧马达
BI2 = Pin('B15',Pin.OUT_PP)

#A电机(右)
p1 = Pin('B8') 
tim1 = Timer(10, freq=120)                  
ch1 = tim1.channel(1, Timer.PWM, pin=p1)
#B电机(左)
p2 = Pin('B9') 
tim2 = Timer(4, freq=120)                  
ch2 = tim2.channel(4, Timer.PWM, pin=p2)

#小车状态
class Car():          #把小车行驶状态存入Car类中
    def go(speed):    #直行状态                
            ch1.pulse_width_percent(speed)   
            ch2.pulse_width_percent(speed)
            AI1(0)        
            AI2(1)
            BI1(1)
            BI2(0)
      
    def back(speed):  #逆行
            ch1.pulse_width_percent(speed)
            ch2.pulse_width_percent(speed)
            AI1(1)
            AI2(0)
            BI1(0)
            BI2(1)

    def stopdj(): #停止
            ch1.pulse_width_percent(0)
            ch2.pulse_width_percent(0)
            

    def spin_left(speed): #左旋
            ch1.pulse_width_percent(0)#右
            ch2.pulse_width_percent(speed)
            AI1(0)
            AI2(0)
            BI1(1)
            BI2(0)

    def spin_right(speed):#右旋
            ch1.pulse_width_percent(speed)
            ch2.pulse_width_percent(0)
            AI1(1)
            AI2(0)
            BI1(0)
            BI2(0)

    def left(speed):      #左转
            ch1.pulse_width_percent(0)#右
            ch2.pulse_width_percent(speed)
            AI1(0)
            AI2(0)
            BI1(1)
            BI2(0)

    def right(speed):     #右转
            ch1.pulse_width_percent(speed)
            ch2.pulse_width_percent(0)
            AI1(1)
            AI2(0)
            BI1(0)
            BI2(0)
Published 166 original articles · 22 praises · 10,000+ views

Guess you like

Origin blog.csdn.net/weixin_45020839/article/details/105660405