|
這個範例可利用伺服馬達來控制前輪轉向的車子
SC8_PS2_MR2x30A範例.inb
(2.85 KB, 下載次數: 10)
Peripheral myS As ServoRunner8A @ 0 '設定模組編號為0,注意!使用Servo Commander8
'上的伺服機相關指令,一定要設定編號為0
Peripheral MRC As MR2x30a @ 1 '設定馬達模組編號為1
Peripheral Ps As GamePadPs @ 31 '設定PS2遙控器模組編號為31
Dim b4Way As Word
Dim wStatus,wKeyStatus As Word
Dim POSx,POSy,P_x,P_y As Integer
Dim Dir,Power As Integer
Sub MAIN()
Ps2Set()
myS.SetPos(0,1500) '設定0號伺服馬達到中央點
myS.SetPos(1,1500) '設定1號伺服馬達到中央點
myS.RunAllServo() '命令伺服馬達開始動作
wKeyStatus = 0
Do
Dir = POSx * 2 '將左搖桿(轉向)動作值放大2倍(可自行調整倍率)
myS.SetPos(0,1500 + Dir) '設定0號伺服馬達轉向角度
myS.SetPos(1,1500 + Dir) '設定1號伺服馬達轉向角度
myS.RunAllServo() '命令伺服馬達開始動作
Power = P_y * 6 '將右搖桿(前後)動作值放大6倍(可自行調整倍率)
If Power > 10 Then
MRC.ForwardA(Power)
Elseif Power < -10 Then
MRC.BackwardA(-Power)
Else
MRC.BrakeA()
End If
Loop
End Sub
'===================PS2搖桿副程式區====================
Sub Ps2Set()
Ps.SetLStickDeadZone(1,5) 'SetDeadZone
Ps.SetRStickDeadZone(1,5) 'SetDeadZone
Ps.SetAnalog(1)
Ps.SetStickRefreshRate(10)
ps.EnableLStickEvent()
ps.EnableRStickEvent()
Ps.EnableKeyPressedEvent() 'Enable Pressed
Ps.EnableKeyReleasedEvent() 'Enable Pressed
End Sub
Event Ps.LStickEvent()
Ps.GetLXYPos(POSx,POSy)
End Event
Event Ps.RStickEvent()
Ps.GetRXYPos(P_x,P_y)
End Event
Event Ps.KeyPressedEvent()
wStatus = Ps.GetKeyStatus()
wKeyStatus = wStatus +b4Way*4096
End Event
Event Ps.KeyReleasedEvent()
wStatus = Ps.GetKeyStatus()
wKeyStatus = wStatus +b4Way*4096
End Event
|
|