Salted fish ZTMR example-remote control steering gear

Salted fish ZTMR example-remote control steering gear

The remote control car is so simple, let's try to see how to control the steering gear. Lay the foundation for the robotic arm.


SG90 servo
range 180 ° (-90 ° ~ 90 °)
Insert picture description here

Pin Explanation
Dark gray (brown) GND
red 5V
Orange X1

PS2 controller
Insert picture description here

PS2 handle digital Representative button Car status
5 on Anticlockwise rotation
7 under clockwise rotation

Routine: remote control single servo
main.py

# main.py -- put your code here!
import ps2
from pyb import Servo,delay


s1=Servo(1)      #定义舵机X1
s1.angle(0)      #开机角度归0
# 一个记录转动角度的函数.这个函数留着 有大用处
def setServoTurn(flag):      
    turn_angle = s1.angle()   
    if flag:
        #逆时针 值递增 最大值90度
        turn_angle += 10 #每按一次转10度
        if turn_angle <= 90:
            s1.angle(turn_angle)
    else:
        #顺时针 值递减 最小值-90度
        turn_angle -= 10
        if turn_angle >= -90:
            s1.angle(turn_angle)
while True: 
    pyb.delay(300)    
    print(ps2.ps2_key()) 
    if (ps2.ps2_key()==5):    #上建逆时针转
        setServoTurn(True)    #Ture调用函数flag的if部分     
    elif (ps2.ps2_key()==7):  #下 顺时针转
        setServoTurn(False)   #False调用函数flag的else部分 

reference
Insert picture description here

ps2.py

hon
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

effect
Insert picture description hereInsert picture description here

Published 166 original articles · 22 praises · 10,000+ views

Guess you like

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