BrianPeek / legoev3

LEGO MINDSTORMS EV3 API for .NET
Apache License 2.0
137 stars 73 forks source link

Run Program on Brick from SDK #1

Open BrianPeek opened 9 years ago

BrianPeek commented 9 years ago

This issue was imported from CodePlex

peekb wrote 2013-11-22 at 09:46 Is it possible to run a program on the brick from a Command?

LA_Guy wrote 2014-01-03 at 13:36 Lego provides a graphical drag and drop programming language which is downloaded to the Brick and run.

Is this possible using .Net EV3 API ?

Thanks

EmGooser commented 8 years ago

I have a number of programs I made with the Mindstorm EV3 pc app and I would like to be able to upload and run them from my own c# application using the API here.

So, is there a way to do this?

geekygravity commented 8 years ago

@EmGooser, have a look at pull request #18

EmGooser commented 8 years ago

Awesome! I will look at it tonight once I get home from work. Thank you @geekygravity

EmGooser commented 8 years ago

I have been trying to get to get the mew method "StartProgramAsync" to work but it seems to put my brick into a bad state. I have to hard reset it by removing a battery.

After uploading my program with the LEGO MINDSTORM EV3 Home Edition called "ReleaseCard" using the download button. It put the program in the "evsfunctions" folder on the device from what I can see on the brick LCD, however there is also a "Project" folder that it is in as well. The file was uploaded as "ReleaseCard" and the LCD displays "Release Card" so I tried the following.

  1. LegoBrick.DirectCommand.StartProgramAsync("../prjs/Project/ReleaseCard");
  2. No action
  3. LegoBrick.DirectCommand.StartProgramAsync("../prjs/Project/Release Card");
  4. Causes brick to freeze
  5. LegoBrick.DirectCommand.StartProgramAsync("../prjs/evsfunctions/ReleaseCard");
  6. No action
  7. LegoBrick.DirectCommand.StartProgramAsync("../prjs/evsfunctions/Release Card");
  8. Causes brick to freeze

When the brick freezes I once got an IO error, but otherwise I get no other debug information. I tested the program I am trying to run via code by running it from the LCD display so I know it is uploaded and works.

Do you have a sample I can work from or any ideas that might help me get this to work?

geekygravity commented 8 years ago

Hi @EmGooser, have you tried including the .rbf file extension in the filepath? The /Project/ in my example represents the 'Project Tile' I set inside the EV3 Home Edition Software before downloading the .rbf files to the brick. I may have some time later today to put together a step-by-step.

EmGooser commented 8 years ago

Just tried both of these and both of them froze the brick

LegoBrick.DirectCommand.StartProgramAsync("../prjs/evsfunctions/ReleaseCard.rbf"); LegoBrick.DirectCommand.StartProgramAsync("../prjs/Project/ReleaseCard.rbf");

Here is the IO exception text though. Looks like a general device disconnection

"An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll

Additional information: The device is not connected."

geekygravity commented 8 years ago

@EmGooser I suspect any connection errors you see occur after the brick crash, rather than the cause. Most of my brick crashes during testing were due to incorrect file paths. Does your path have spaces? If so I think the EV3 Home Edition Software converts spaces to underscores when downloading programs to the brick. I'm working on an example demo for you in the meanwhile.

geekygravity commented 8 years ago

@EmGooser also, as I'm testing incorrect path crashes, I've had to delete the Bluetooth connection on my phone and re-pair the Bluetooth connection after every crash, even if the devices think they are still paired. This is only necessary after a brick crash. What device and connection method are you using?

EmGooser commented 8 years ago

I figured the IO error was more of a result of the freeze itself and not the cause, but wanted to report it to be sure.

I am using a USB cable to connect with it on a windows 10 box. Visual Studio 2015 in a c# WPF application.

It actually didn't let me put in spaces when I was uploading it, however I renamed it without the space then reuploaded it. After I verified that it worked via the brick I tried this again.

LegoBrick.DirectCommand.StartProgramAsync("../prjs/evsfunctions/ReleaseCard");

No crash, but it still failed to run the program.

EmGooser commented 8 years ago

Clarification

The upload asked for a file name and that is what was not allowing spaces I renamed the actual project name that is so it shows up on the brick LCD without a space too

geekygravity commented 8 years ago

@EmGooser, I'm currently debugging this. I am able to start a program via Bluetooth from an Android or Windows phone without issue. I'm also able to start and run program via USB, however there appears to be a USB communication error at some point while the program is running. This is unexpected and I'm currently investigating. I may have a few test files for you sometime tomorrow.

EmGooser commented 8 years ago

Awesome. I assume you are doing this on your free time so no rush. Thank you for the assist :)

geekygravity commented 8 years ago

@EmGooser

Thanks, this is all personal free time. Start Program works for me on Bluetooth. As an example, download the EV3 program from my Notebook and use this filepath: ("../prjs/Barn_Door_Tracker/Program-01.rbf");

However USB has an issue similar to issue #7. To debug I suppressed the 'KeyNotFoundException' error via disabling polling in Brick.cs by changing "if (pollingTime != TimeSpan.Zero)" to "if(false)" This shows the EV3 Program will start and run for the initial 10 second delay before USB disconnects after the motor moves. I suspect a brick change event crashes the USB connection, which also crashes the brick, but I've been unable to track it down. I've uploaded the MainWindow XAML and CS WPF files for you as an example.

EmGooser commented 8 years ago

hum... I am just making a demo right now so I might try to find a way to use a bluetooth connection for right now. I will take a look at the xaml and cs files you uploaded this weekend.

geekygravity commented 8 years ago

@EmGooser

I've tested this with Bluetooth on Windows 8.1 desktop, Windows Phone 8.1, and an Android phone. All of these work as expected in my case. For Windows 8.1 desktop, confirm the COM number after you pair the EV3. You also need to run Visual Studio in admin mode for proper permission to access Bluetooth.

To test this on Windows 8.1 desktop, I bought a tiny Bluetooth adapter that plugs into where the USB cable would go. It was about $16.40 price matched online. Kensington Bluetooth 4.0 USB Adapter. I download drivers from Kensington website. The packaging claims it is Windows 10 Compatible.

EmGooser commented 8 years ago

I got a bluetooth paired and working, after getting it all setup in my app it worked like a charm. This will work for now. Thank you :)

geekygravity commented 8 years ago

@EmGooser Awesome! Good to hear.