This project has moved and is read-only. For the latest updates, please go here.

Code not working.

Mar 12, 2014 at 11:39 PM
I have modified the example remote code provided in the video. I have xaml properties for both Left and Right mouse click on the buttons, and on release of the buttons. For some reason, the right mouse button code works, but the left does not. Also, as I increase the power output to 100%, the EV3 acts erratic. I am going to include the .cs backend to the xaml form, if anyone can help me understand why I am having such strange issues, it would be greatly appreciated.

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;

namespace LegoAPI_Remote
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    Brick _brick;
    int _forward = 30;
    int _backward = -20;
    int _forwardslow = 40;
    int _backwardslow = -30;      

    public MainWindow()
    {
        InitializeComponent();
    }

    private async void Window_Loaded(object sender, RoutedEventArgs e)
    {
        _brick = new Brick(new BluetoothCommunication("COM1"));
        _brick.BrickChanged += _brick_BrickChanged;
        await _brick.ConnectAsync();
        await _brick.DirectCommand.PlayToneAsync(100, 1000, 300);
        await _brick.DirectCommand.SetMotorPolarity(OutputPort.A | OutputPort.B, Polarity.Forward);
        await _brick.DirectCommand.StopMotorAsync(OutputPort.All, false);
        await _brick.DirectCommand.ClearChanges(InputPort.Three);
        _brick.Ports[InputPort.Three].SetMode(TouchMode.Bumps);
    }

    private void _brick_BrickChanged(object sender, BrickChangedEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("Brick Changed!");          
    }




    private async void ForwardButton_Left_Click_Down(object sender, RoutedEventArgs e)
    {
        await _brick.DirectCommand.TurnMotorAtPowerAsync(OutputPort.A | OutputPort.B, _forward);
    }
    private async void ForwardButton_Left_Click_Up(object sender, RoutedEventArgs e)
    {
        await _brick.DirectCommand.StopMotorAsync(OutputPort.All, false);
    }
    private async void ForwardButton_Right_Click_Down(object sender, RoutedEventArgs e)
    {
        await _brick.DirectCommand.TurnMotorAtPowerAsync(OutputPort.A | OutputPort.B, _forwardslow);
    }
    private async void ForwardButton_Right_Click_Up(object sender, RoutedEventArgs e)
    {
        await _brick.DirectCommand.StopMotorAsync(OutputPort.All, false);
    }
    private async void BackwardsButton_Left_Click_Down(object sender, RoutedEventArgs e)
    {
        await _brick.DirectCommand.TurnMotorAtPowerAsync(OutputPort.A | OutputPort.B, _backward);
    }
    private async void BackwardsButton_Left_Click_Up(object sender, RoutedEventArgs e)
    {
        await _brick.DirectCommand.StopMotorAsync(OutputPort.All, false);            
    }
    private async void BackwardsButton_Right_Click_Down(object sender, RoutedEventArgs e)
    {
        await _brick.DirectCommand.TurnMotorAtPowerAsync(OutputPort.A | OutputPort.B, _backwardslow);
    }
    private async void BackwardsButton_Right_Click_Up(object sender, RoutedEventArgs e)
    {
        await _brick.DirectCommand.StopMotorAsync(OutputPort.All, false);
    }        
    private async void LeftButton_Left_Click_Down(object sender, RoutedEventArgs e)
    {              
        await _brick.DirectCommand.TurnMotorAtPowerAsync(OutputPort.A, _forward);
        await _brick.DirectCommand.TurnMotorAtPowerAsync(OutputPort.B, _backward);
    }
    private async void LeftButton_Left_Click_Up(object sender, RoutedEventArgs e)
    {
        await _brick.DirectCommand.StopMotorAsync(OutputPort.All, false);
    }
    private async void LeftButton_Right_Click_Down(object sender, RoutedEventArgs e)
    {         
        await _brick.DirectCommand.TurnMotorAtPowerAsync(OutputPort.A, _forwardslow);
        await _brick.DirectCommand.TurnMotorAtPowerAsync(OutputPort.B, _backwardslow);
    }
    private async void LeftButton_Right_Click_Up(object sender, RoutedEventArgs e)
    {
        await _brick.DirectCommand.StopMotorAsync(OutputPort.All, false);
    }
    private async void RightButton_Left_Click_Down(object sender, RoutedEventArgs e)
    {
        await _brick.DirectCommand.TurnMotorAtPowerAsync(OutputPort.B, _forward);
        await _brick.DirectCommand.TurnMotorAtPowerAsync(OutputPort.A, _backward);
    }
    private async void RightButton_Left_Click_Up(object sender, RoutedEventArgs e)
    {
        await _brick.DirectCommand.StopMotorAsync(OutputPort.All, false);
    }
    private async void RightButton_Right_Click_Down(object sender, RoutedEventArgs e)
    {
        await _brick.DirectCommand.TurnMotorAtPowerAsync(OutputPort.B, _forwardslow);
        await _brick.DirectCommand.TurnMotorAtPowerAsync(OutputPort.A, _backwardslow);
    }
    private async void RightButton_Right_Click_Up(object sender, RoutedEventArgs e)
    {
        await _brick.DirectCommand.StopMotorAsync(OutputPort.All, false);
    }
    private async void LiftUpButton_Left_Click_Down(object sender, RoutedEventArgs e)
    {
        await _brick.DirectCommand.TurnMotorAtPowerAsync(OutputPort.C, _forward);
    }
    private async void LiftUpButton_Left_Click_Up(object sender, RoutedEventArgs e)
    {
        await _brick.DirectCommand.StopMotorAsync(OutputPort.All, false);
    }
    private async void LiftUpButton_Right_Click_Down(object sender, RoutedEventArgs e)
    {
        await _brick.DirectCommand.TurnMotorAtPowerAsync(OutputPort.C, _forwardslow);
    }
    private async void LiftUpButton_Right_Click_Up(object sender, RoutedEventArgs e)
    {
        await _brick.DirectCommand.StopMotorAsync(OutputPort.All, false);
    }
    private async void LiftDownButton_Left_Click_Down(object sender, RoutedEventArgs e)
    {
        await _brick.DirectCommand.TurnMotorAtPowerAsync(OutputPort.C, _backward);
    }
    private async void LiftDownButton_Left_Click_Up(object sender, RoutedEventArgs e)
    {
        await _brick.DirectCommand.StopMotorAsync(OutputPort.All, false);
    }
    private async void LiftDownButton_Right_Click_Down(object sender, RoutedEventArgs e)
    {
        await _brick.DirectCommand.TurnMotorAtPowerAsync(OutputPort.C, _backwardslow);
    }
    private async void LiftDownButton_Right_Click_Up(object sender, RoutedEventArgs e)
    {
        await _brick.DirectCommand.StopMotorAsync(OutputPort.All, false);
    }
    private async void SweepLeftButton_Left_Click_Down(object sender, RoutedEventArgs e)
    {
        await _brick.DirectCommand.TurnMotorAtPowerAsync(OutputPort.D, _forward);
    }
    private async void SweepLeftButton_Left_Click_Up(object sender, RoutedEventArgs e)
    {
        await _brick.DirectCommand.StopMotorAsync(OutputPort.All, false);
    }
    private async void SweepLeftButton_Right_Click_Down(object sender, RoutedEventArgs e)
    {
        await _brick.DirectCommand.TurnMotorAtPowerAsync(OutputPort.D, _forwardslow);
    }
    private async void SweepLeftButton_Right_Click_Up(object sender, RoutedEventArgs e)
    {
        await _brick.DirectCommand.StopMotorAsync(OutputPort.All, false);
    }
    private async void SweepRightButton_Left_Click_Down(object sender, RoutedEventArgs e)
    {
        await _brick.DirectCommand.TurnMotorAtPowerAsync(OutputPort.D, _backward);
    }
    private async void SweepRightButton_Left_Click_Up(object sender, RoutedEventArgs e)
    {
        await _brick.DirectCommand.StopMotorAsync(OutputPort.All, false);
    }
    private async void SweepRightButton_Right_Click_Down(object sender, RoutedEventArgs e)
    {
        await _brick.DirectCommand.TurnMotorAtPowerAsync(OutputPort.D, _backwardslow);
    }
    private async void SweepRightButton_Right_Click_Up(object sender, RoutedEventArgs e)
    {
        await _brick.DirectCommand.StopMotorAsync(OutputPort.All, false);
    }
}
}
Mar 17, 2014 at 9:52 PM
Without the XAML and a repro project I can't figure out exactly what you're trying to do, but I can say that the places where you're calling 2 DirectCommand's in sequence, instead use a BatchCommand to send both commands at once. If you can provide a link to a .zip of your project, I can attempt to figure out what's going on with the left vs. right mouse button.