1

最近我一直在为 ROV 项目编写代码。我的目标是配置一个 XBOX 360 控制器来控制整个事情。到目前为止,一切都很好,除了操纵杆!我确实让它在某种程度上适用于驱动电机,但我希望有人可以快速查看代码,看看是否有更有效或更可靠的方法来做到这一点。我发现有时候我快速拨动摇杆时,当摇杆居中到 0 时电机不熄火。这个项目的问题是它将用于比赛,我想确定一下比赛期间不躲避我。无论如何,这是我目前正在使用的代码。我唯一真正关心的是操纵杆。

Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Input
Public Class Form1
Dim WithEvents MC1 As Phidget21COM.PhidgetMotorControl
Dim WithEvents MC2 As Phidget21COM.PhidgetMotorControl

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    'Kills the power to the motors and disconnects the Phidget.
    MC1.Velocity(0) = 0
    MC1.Velocity(1) = 0
    MC1.Close()
    MsgBox("Now safe to remove devices")
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Creates a new instance of the motor controller (For drives)
    MC1 = New Phidget21COM.PhidgetMotorControl
    MC1.Open(15100)
    MC1.WaitForAttachment(3000)
    'Checks phidget attached status
    If MC1.IsAttached = True Then
        CheckBox2.Checked = True
    Else
        CheckBox2.Checked = False
        MsgBox("Connection could not be established. Check hardware and restart the program.")
    End If
    'Checks joystick attached status
    JoyConnect.Text = ""
    Threading.Thread.Sleep(1000)
    If GamePad.GetState(PlayerIndex.One).IsConnected = True Then
        JoyConnect.Text = "Controller Connected!"
        CheckBox1.Checked = True
    Else
        JoyConnect.Text = "Not Connected. :("
    End If

End Sub

Private Sub JoySticks()

    'Left Joystick
    Dim LY As Integer
    Dim LX As Integer
    LY = GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.Y * 100
    LX = GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X * 100
    LX1.Text = LX
    LY1.Text = LY
    If LY > 0 Then
        MC1.Velocity(0) = LY
        MC1.Velocity(1) = LY
    ElseIf LY < 0 Then
        MC1.Velocity(0) = LY
        MC1.Velocity(1) = LY
    ElseIf LX > 0 Then
        MC1.Velocity(0) = LX
        MC1.Velocity(1) = LX * -1
    ElseIf LX < 0 Then
        MC1.Velocity(0) = LX
        MC1.Velocity(1) = LX * -1
    ElseIf LX Or LY = 0 Then           '<--- Seemed to fix some issues with
        MC1.Velocity(0) = 0            'the motors not stopping when the stick
        MC1.Velocity(1) = 0            'centred to 0.
    End If

    'Right Joystick
    Dim RY As Integer
    Dim RX As Integer
    RY = GamePad.GetState(PlayerIndex.One).ThumbSticks.Right.Y * 100
    RX = GamePad.GetState(PlayerIndex.One).ThumbSticks.Right.X * 100
    RY1.Text = RY
    RX1.Text = RX



End Sub

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    'Frequently updates the joysticks and motors. 1000 Updates/Second. (1ms)
    JoySticks()
End Sub
End Class
4

0 回答 0