使用 C++,我将如何从没有运行乐高软件的机器向我的 EV3/nxt 发送命令?我想控制我的头脑风暴机器人的向前、向后、向左和向右方向。我可以加载砖以响应某些字节命令,然后通过蓝牙从那台不同的机器发送这些字节命令吗?
任何建议将不胜感激,谢谢!
这是我在 EV3 上运行的一些 c# 代码。我只是想弄清楚如何将相同的功能加载到我的砖上,然后通过像本文档中的一些字节命令来触发它。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Lego.Ev3.Core;
using Lego.Ev3.Desktop;
using System.Diagnostics;
namespace legoAPIDemo
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Brick _brick;
int _forward = 40;
int _backward = -30;
uint _time = 300;
public MainWindow()
{
InitializeComponent();
}
private async void Window_Loaded(object sender, RoutedEventArgs e)
{
_brick = new Brick(new BluetoothCommunication("COM3"));
_brick.BrickChanged += _brick_BrickChanged;
await _brick.ConnectAsync();
await _brick.DirectCommand.PlayToneAsync(100, 1000, 300);
await _brick.DirectCommand.SetMotorPolarity(OutputPort.B | OutputPort.C, Polarity.Backward);
await _brick.DirectCommand.StopMotorAsync(OutputPort.All, false);
}
private void _brick_BrickChanged(object sender, BrickChangedEventArgs e)
{
Debug.WriteLine("BRICK CHANGED!");
}
private async void ForwardButton_Click(object sender, RoutedEventArgs e)
{
await _brick.DirectCommand.TurnMotorAtPowerForTimeAsync(OutputPort.B | OutputPort.C, _forward, _time, false);
}
private async void BackButton_Click(object sender, RoutedEventArgs e)
{
await _brick.DirectCommand.TurnMotorAtPowerForTimeAsync(OutputPort.B | OutputPort.C, _backward, _time, false);
}
private async void LeftButton_Click(object sender, RoutedEventArgs e)
{
_brick.BatchCommand.TurnMotorAtPowerForTime(OutputPort.B, _backward, _time, false);
_brick.BatchCommand.TurnMotorAtPowerForTime(OutputPort.C, _forward, _time, false);
await _brick.BatchCommand.SendCommandAsync();
}
private async void RightButton_Click(object sender, RoutedEventArgs e)
{
_brick.BatchCommand.TurnMotorAtPowerForTime(OutputPort.C, _forward, _time, false);
_brick.BatchCommand.TurnMotorAtPowerForTime(OutputPort.B, _backward, _time, false);
await _brick.BatchCommand.SendCommandAsync();
}
}
}