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

Can't get an updated value from my infrared sensor

Jan 8, 2014 at 7:30 PM
Hi,

I tried to let my robot walked while is not in a wall and I am not able to get an updated value from my infrared sensor.
        float distanceObjet = _brick.Ports[InputPort.Four].SIValue;
        bool bMissionAccomplie = false;

        while (bMissionAccomplie == false)
        {
await _brick.DirectCommand.TurnMotorAtPowerForTimeAsync(OutputPort.B | OutputPort.C, VITESSEAVANCE, TEMPSCOMMANDE, false);
            distanceObjet = _brick.Ports[InputPort.Four].SIValue;

            if (distanceObjet < 20)
                bMissionAccomplie = true;
        }
am I doing it right?
Best regards,
Hugo
Jan 9, 2014 at 4:14 AM
Can you send the rest of your project/source code? Hard to tell if that's right or wrong...it may be right or wrong depending on the rest of the program. :)
Jan 9, 2014 at 4:32 AM
Hi,
Thanx for your help :-)
Here's my code:
public partial class frmControleMindstormEv3 : Form
{
    private Brick _brick;
    /// <summary>
    /// Constructeur
    /// </summary>
    public frmControleMindstormEv3()
    {
        InitializeComponent();
    }

    /// <summary>
    /// Sur le chargement de la form, initialiser la brique et faire jouer un son.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private async void Form1_Load(object sender, EventArgs e)
    {
        //Création de l'objet brique en définissant le mode de connexion
        _brick = new Brick(new UsbCommunication());
        //Définir la méthode qui interceptera les changement de la brique
        _brick.BrickChanged += Brick_Changed;
        //Connexion à la brique. La connexion se fait de manière asynchrone, mais nous allons tout de même attendre la connexion
        await _brick.ConnectAsync();
        await _brick.DirectCommand.ClearAllDevices();
        await _brick.DirectCommand.StopMotorAsync(OutputPort.All, false);

    }

    /// <summary>
    /// Cette méthode sera appelé lorsqu'une information sur un port aura changé.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Brick_Changed(object sender, BrickChangedEventArgs e)
    {
        txtDistance.Text = "SIValue " + e.Ports[InputPort.Four].SIValue.ToString() + "\r\n";
        txtDistance.Text += "PercentValue " + e.Ports[InputPort.Four].PercentValue.ToString() + "\r\n";
        txtDistance.Text += "RawValue " + e.Ports[InputPort.Four].RawValue.ToString() + "\r\n";
    }

    /// <summary>
    /// Avance jusqu'au mur et arrête.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private async void btnAvanceMur_Click(object sender, EventArgs e)
    {
        float distanceObjet = _brick.Ports[InputPort.Four].SIValue;
        bool bMissionAccomplie = false;

        while (bMissionAccomplie == false)
        {
            await _brick.DirectCommand.TurnMotorAtPowerForTimeAsync(OutputPort.B | OutputPort.C, 40, 200, false);
            distanceObjet = _brick.Ports[InputPort.Four].SIValue;

            if (distanceObjet < 20)
                bMissionAccomplie = true;
        }
    }


}
Best regards,
Hugo
Jan 9, 2014 at 5:48 AM
Just tested here and it works exactly as expected, but, as it states in the "Known Issues" section of the docs lined above, USB support on desktop is very, very flaky. The data coming back was slow to update. I highly recommend using Bluetooth or Wifi. You may not be getting data fast enough (or at all) depending on your machine. If you can, try one of the other connection methods (or even platforms...WinRT if you're running 8.1 or Phone if you have one) and see if the issue persists.
Jan 15, 2014 at 2:35 AM
Hi peekb,

Well it did the same thing with the Bluetooth. I had to added a Thread.Sleep(10); and it works fine. Thanx for your help.

Best regards,
Hugo St-Louis
Jan 17, 2014 at 7:22 PM
Hi Peekb,

Do you know what's the best way to wait until the end of a motor execution?

Best regards,
Hugo
Jan 17, 2014 at 7:48 PM
Best way I know of is to use the BrickChanged event which fires at a default of 100ms and grab the current motor position. When it hits the desired position, execute the next command.
May 6, 2014 at 12:10 AM
Hi all,

USB connection seems to become much more stable if you Flush() the stream after each command. At least it solved all my problems.
        public async Task WriteAsync(byte[] data)
        {
            data.CopyTo(_outputReport, 1);
            await _stream.WriteAsync(_outputReport, 0, _outputReport.Length);
            _stream.Flush();
        }
Mar 23, 2015 at 6:55 PM
I didn't write the code, but if you want to wait for motor execution to finish, the following definitely helped me:

https://legoev3.codeplex.com/SourceControl/network/forks/ahilevich/ev3usb/changeset/a23ec8f07efffae344b2cef6a9d1daef7c242d2d

It's a little extra effort to copy the code and modify the necessary references, but it was definitely worth it!