How can I connect a EV3 mindstorms via bluotooth to a unity game using unityscript? - bluetooth

I am making a race game in Unity with Unityscript and I made a steer with the lego mindstorms EV3 robot. I let te robot send information by bluetooth to the game, but I can't find how I can do that.
I already have the code for the bluetooth running and working a C#, but know I need to know how to translate it to unityscript.
I already tried to find it on google, but I only seem to get some software to hack the robot, but not to make code in unityscript for connecting the steer.
Under here stands the C# code:
// EV3: The EV3Messenger is used to communicate with the Lego EV3.
private EV3Messenger ev3Messenger;
// EV3: Create an EV3Messenger object which you can use to talk to the EV3.
ev3Messenger = new EV3Messenger();
// EV3: Connect to the EV3 serial port over Bluetooth.
// If the program 'hangs' on a call to ev3Messenger.Connect,
// then your EV3 is not paired with your PC yet/anymore.
// To pair: Remove the EV3 from the Windows Bluetooth device list and add it again.
ev3Messenger.Connect("COM3"); // Hardcoded serial port: put the serial port
// of the Bluetooth connection to your EV3 here!
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// Unload any non ContentManager content here
// EV3: Disconnect
if (ev3Messenger.IsConnected)
{
ev3Messenger.Disconnect();
}
}
// EV3: send Brake message to mailbox with name "MakeNoise"
if (ev3Messenger.IsConnected)
{
ev3Messenger.SendMessage("MakeNoise", "Brake");
}
// Game can be controlled by both the arrow keys and the Steer, gas and brake paddle of the connected EV3
UpdatePaddlePositionUsingKeys();
UpdatePaddlePositionUsingEV3();
base.Update(gameTime);
}
///Steer update
private void UpdatePaddlePositionUsingEV3()
{
if (ev3Messenger.IsConnected)
{
// EV3: Receive a new command from mailbox "COMMAND" of the EV3
// and use it to change the direction of the paddle or to exit the game.
EV3Message message = ev3Messenger.ReadMessage();
if (message != null
&& message.MailboxTitle == "Command")
{
if (message.ValueAsText == "")
{
}
{
ev3Messenger.Disconnect();
Exit();
}
}
}
}
I hope that you know where I can find how I can do this or even help me further.
If you want the original code for a small pong game where I got my inspiration from, just comment it.
I hope you can help me.

Here are some useful links with documentation on the EV3 firmware :
Firmware Documentation
LMS2012 Documentation
HDK/SDK
In particular, you need to learn how to send direct commands and then use that to read and write bluetooth mailboxes.
For communicating with the COM port itself using javascript, just do a little searching. For example, I found this SO question which has quite a few different ideas.

As part of c4ev3, We open-sourced our EV3 uploader, which can also be used to send connection-agnostic commands to the device.
Here is how you would move the motors in Perl (Complete version):
use IPC::Open2;
print open2(\*EV3OUT, \*EV3IN, "ev3 tunnel") or die "couldn't find: %!";
print EV3IN "0900xxxx8000 00 A3 00 09 00\n";
print EV3IN "0C00xxxx8000 00 A4 00 09 50 A6 00 09\n";
This would probe for an EV3 accessible over USB, Bluetooth or WiFi and connect to it, then send the direct messages associated with turning the motors. For more information on the direct commands protocol check out LEGO's Communication Developer Manual and David Lechner's answer.
Alternatively, you can write a C program for the EV3 with c4ev3 and communicate with that. That way you got a nicer looking C-API you can use.

Related

Is it possible to scan barcodes into a process in the background?

I'm making a gym management web app that handles sign-ins. Members have a barcode on a tag that they scan when they arrive to the gym.
I've heard that most barcode scanners simply act as a keyboard. This would require the scanning-in page to be open and in the foreground when a barcode is scanned.
If it's just a keyboard, how would I send the barcode scanner input to a single background process running on the computer, and have it ignore by all processes that may be in focus?
You're right that most scanner can support HID in keyboard emulation, but that's just the start.
If you want to have a bit more control over the data you can use a scanners that support the OPOS driver model.
Take a look at Zebra's Windows SDK to have a overview of the things that you can do. It may be a better solution than try to steal the barcode data coming in the OS as a keyboard entry to the foreground app.
Disclaimer: I work for Zebra Technologies
Other Barcode scanner vendor support a similar driver model.
I found an interesting post with a simple solution:
On the form constructor
InitializeComponent():
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form1_KeyPress);
Handler & supporting items:
DateTime _lastKeystroke = new DateTime(0);
List<char> _barcode = new List<char>(10);
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
// check timing (keystrokes within 100 ms)
TimeSpan elapsed = (DateTime.Now - _lastKeystroke);
if (elapsed.TotalMilliseconds > 100)
_barcode.Clear();
// record keystroke & timestamp
_barcode.Add(e.KeyChar);
_lastKeystroke = DateTime.Now;
// process barcode
if (e.KeyChar == 13 && _barcode.Count > 0) {
string msg = new String(_barcode.ToArray());
MessageBox.Show(msg);
_barcode.Clear();
}
}
Credits: #ltiong_sh
Original post: Here
Use RawInput API (https://www.codeproject.com/Articles/17123/Using-Raw-Input-from-C-to-handle-multiple-keyboard#_Toc156395975) and check device ID for incoming keystrokes. Different devices have different IDs. You can also block keystrokes from scanner from reaching your application and interfering with input fields.
One thing you might want to add is option for user to identity which device is used as a barcode scanner. I did it by asking user to test-scan barcode with scanner on first application startup or in settings.
Works with any barcode scanner which outputs keystrokes.

Arduino - How to read a string from the Serial Port

I just recently started working with Arduino. I just have a quick question, I tried searching for an answer but have failed for days. Basically what I wanna ask is if there is a way to read a whole line from the Serial Port. Like the line highlighted in the picture below.
What I'm trying to do is using a Bluesmirf Silver Rn-42 to search the area for a bluetooth device and trigger a signal if a matching address is found. I just cant figure out how to read messages that are already on the Serial port.
Use .readString()
Example code:
String myString;
void setup()
{
Serial.begin(9600);
}
void loop()
{
while (Serial.available())
{
myString = Serial.readString();
//do stuff with the string
}
}
If you want to read something that's already in the serial port from the Arduino end, then you need to rethink your code. Anything you produce within your code to print to the serial monitor will already be in your program ready to access if you make it available in the right way. The exemplar string you provided, is simply an array of characters that you can store in an element within an array, making it accessible whenever you need it.
Hints:
Never read back from the serial monitor, it's really slow -.-
Make all the resources you require accessible and available in memory at the time you need it to save hassel & processing power.
Never make the same mistake twice.
However, if you want to read from the COM port that the Arduino is connected to in Windows, then you'll need to work with Libusb libraries found here: http://www.libusb.org/ for C. Any other language will be library or import dependent.

Control lego NXT using Arduino [Bluetooth]

I am trying to control NXT robot using Arduino UNO and bluetooth, I used this code
#include <SoftwareSerial.h>
byte moveTelegram [] = {0x0C,0x00,0x80,0x04,0x01,0x32,0x05,0x01,0x00,0x20,0x00,0x00,0x00,0x00};
SoftwareSerial blue(10, 11);
int BluetoothData;
void setup()
{
blue.begin(9600);
}
void loop()
{
blue.write(moveTelegram,sizeof(moveTelegram));
delay(100);
BluetoothData=blue.read();
delay(2000);
}
My problem is , I have to send data from NXT to Arduino, and then the NXT start moving (if I add blue.read() to my code).
How to make the NXT execute the commands directly?
Thanks,
execute the commands directly?
I don't know what's that telegram you pasted, but that must be a kind of protocol defined by your own, so there's no way in NXT side to execute directly, you have to manually resolve that BYTES in NXT side, and then mapping to related NXT control command.
There's a existing official protocol in NXT which called Lego NXT communication protocol, since you're using Arduino, then you lost the convenient to directly call it, and you have read some official doc to assemble these BYTES, but in NXT side, If you're using the leJOS(or the official firmware), this protocol was build in supported, try google, there're lots of post.

Sending commands from custom made Bluetooth device to android phone to control music player

I have created a simple Bluetooth device using following components
HC05 module
Arduino Uno board (with re-programmable micro-controller)
I am wondering if it is possible to send commands from my BT device, as if these commands were sent from Bluetooth headset?
What I mean is:
we send 0x00000055 keycode - and the music pauses
(KEYCODE_MEDIA_PLAY_PAUSE)
we send 0x00000058 - previous song starts playing
(KEYCODE_MEDIA_PREVIOUS)
...
Here is the full list of keycodes which android uses: http://developer.android.com/reference/android/view/KeyEvent.html
I can probably create a separate app, which will read incoming commands and simulate headset button presses, but that is not what I want. As far as I'm concerned - some of the headsets are plug-and-play, meaning that no additional apps must be installed on android device. Here is the code I am currently use to send commands to Android phone:
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX
enum { LED_PIN = 6 };
enum LedState { LED_ON, LED_OFF, LED_BLINK };
LedState led_state;
void setup()
{
led_state = LED_OFF;
pinMode(LED_PIN, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(9, LOW);
Serial.begin(9600);
Serial.println("Enter AT commands:");
BTSerial.begin(38400); // HC-05 default speed in AT command more
}
const int COMMAND_MUSIC = 85;
void loop()
{
digitalWrite(LED_PIN, HIGH);
delay(100);
digitalWrite(LED_PIN, LOW);
delay(100);
delay(10000);
// trying to play or pause once in 10 seconds
BTSerial.write(0x00000055);
//BTSerial.print(0x00000055, HEX);
}
Both devices are paired but music player on my phone stays unaffected by these commands..Is it possible to control music player without creating a side app for "incoming commands from BT"?
Question is if your board supports AVRCP controller BT profile?
If it does you "only" need to connect against your phones AVRCP target BT profile. When you have a AVRCP BT connection there is specified commands how to pause and skip songs.
This is how the "plug and play" headset does.
Read more about Bluetooth profiles.
http://en.wikipedia.org/wiki/Bluetooth_profile
Looking at your code you have set up a serial link towards a phone.
This link uses SPP profile and you will only be able to send raw data over that link.
If this is the only profile that your BT stack on your Arduino Uno board have you will be forced to create an application on the phone side to be able to read the raw data and do something with it e.g. pause music.
Hope this cleared things little for you.
Probably it is to late for you, but maybe I can help someone else.
Firstly, Bluetooth devices like BT headphones, keyboards etc, are known as HID (Human Interface devices). HC05 are not one of this out of the box, but there is a solution introduced by Evan Kale (link: https://www.youtube.com/watch?v=BBqsVKMYz1I) how to update one of this using serial port connection.
Other solution is to buy BT HID module, but they are more expensive (about 10 times)

Checking if the bluetooth module on my Arduino board works

I'm trying to have my Arduino UNO board to work with a BlueSmirf Gold (http://www.sparkfun.com/products/10268).
I wired it as explained on various tutorial (for example here: http://www.instructables.com/id/how-to-Control-arduino-by-bluetooth-from-PC-pock/)
I've set the baud rate to 9600 as explained here: http://forum.sparkfun.com/viewtopic.php?p=94557
I manage to connect to it using the default Arduino serial terminal, ZTerm and my phone (using Amarino). In every cases, the green light on the bluetooth modem turns on, so until there it looks good.
The main problem is that my modem does not seem to be able to send/receive anything (the only time I've had any response was when I've set the baud rate to 9600).
For example, I have this code (simplified here, but the main idea is there):
int out_pin = 2;
String readLine() {
char command[100];
int i = 0;
if(Serial.available()){
delay(100);
while( Serial.available() > 0 && i< 99) {
command[i++] = Serial.read();
}
command[i++]='\0';
Serial.flush();
}
Serial.print("command: ");
Serial.println(command);
return (String) command;
}
void menu() {
if (Serial.available() <= 0) {
return;
}
String command = readLine();
// Do thing based on the command
}
void setup() {
pinMode(out_pin, OUTPUT);
Serial.begin(9600);
}
void loop() {
menu();
}
Logically, when I send something via the terminal, I should get it back (which is what happens when using the usb serial).
When I connect to the board via Bluetooth, it just stays silent.
I also tried this piece of code:
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("Spam ...");
}
Works fine when using the usb serial, but I get nothing when using bluetooth to connect to my board.
With both codes, I also tried to use the monitor tool in Amarino to send messages to the board, but it seems that it never got it.
I've tried various other things:
- do not have the USB serial available (I powered the Arduino board via USB but using a plug wall adapter. I'll try later on with a 9V battery but I don't have it available at the moment)
- do not connect CTS-1 to RTS-0 in the modem (some tutorial tell to connect them, the other don't. So as I doubted I tried both solutions).
The only time I've had something that looked like a communication was with this setup:
Arduino - Phone connected via Bluetooth
The Amarino monitoring was on
Arduino - Computer connected via the USB serial
When uploading the new code to my board, some parts of it were displayed on the monitoring tool on the phone.
It happened one or twice and I can't reproduce it now.
I'm pretty sure I've done something wrong somewhere (it seems at least it's the most logical explanation) but I'm also wondering if it could not be a problem with the Bluetooth model (I mean, even the sample tutorials do not work).
So the questions are:
is there something I missed/forgot to do that could help me solve the
problem ?
if not: is there a simple way to check that my Bluetooth modem
works fine ?
Thanks,
Vincent
I still don't have the answer on the second question ("is there a simple way to check that my Bluetooth modem works fine ?") but I've finally be able to send/receive messages from the Bluetooth modem.
As I was guessing (at least how I understand it) that was a problem with the two serials (Bluetooth and USB) on the same board.
To solve that, I've plugged the BT TX-1 on pin digital 5, RX-0 on digital 3 and used the following code (based on the SoftwareSerial tutorial):
#include <SoftwareSerial.h>
SoftwareSerial bluetooth(5, 3);
void setup()
{
// Start the hardware serial port
Serial.begin(9600);
bluetooth.begin(9600);
}
void loop()
{
bluetooth.listen();
// while there is data coming in, read it
// and send to the hardware serial port:
while (bluetooth.available() > 0) {
char inByte = bluetooth.read();
Serial.write(inByte);
}
}
It sends all entries received from Bluetooth on the default serial (in my case USB).
I've checked with Amarino and messages sent from my phone are displayed in the Arduino serial monitor.
Same problem here. I tried connect vice versa the 0 and 1, RX and TX(i.e. RX to RX and TX to TX) and I got some communication of gibberish as opposed to nothing at all.

Resources