I’m excited to share a clip of my recent project, so here it is!
http://www.youtube.com/watch?v=cHc6vaCsBW4
That is my Nokia Lumia 920 Windows 8 phone that I am using to control sparki. I’m sending commands at 10hz. The commands are ‘F’ (forward), ‘B’ (back), ‘L’ (left), ‘R’ (right), and ‘S’ (stop). That is just a janky custom C# app that I threw together to try to get this up and running as fast as possible. I’m looking forward to improving it.
From here on, I’ve included a bunch of technical details to share…
My sketch is pretty simple (and kind of messy):
[code]#include <Sparki.h> // include the sparki library
int ir_sequence = 0;
int serial_sequence = 0;
int last_ir_sequence = -1;
int last_serial_sequence = -1;
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
delay(100); // delay to establish the usb connection. using the arduino while-serial command doesn’t
// let it boot properly when there’s no USB conenction.
}
char serial_code = ‘0’;
void loop()
{
int ir_code = sparki.readIR();
if(ir_code != -1)
{
ir_sequence++;
}
if(Serial1.available())
{
char serial_code = (char)Serial1.read();
serial_sequence++;
switch(serial_code)
{
case 'F':
sparki.moveForward();
break;
case 'B':
sparki.moveBackward();
break;
case 'L':
sparki.moveLeft();
break;
case 'R':
sparki.moveRight();
break;
case 'S':
sparki.moveStop();
break;
}
}
//
if (last_ir_sequence != ir_sequence || last_serial_sequence != serial_sequence)
{
last_ir_sequence= ir_sequence;
last_serial_sequence = serial_sequence;
sparki.clearLCD();
sparki.print("IR sequence: ");
sparki.println(ir_sequence);
sparki.print("IR code: ");
sparki.println(ir_code);
sparki.println();
sparki.print("Serial sequence: ");
sparki.println(serial_sequence);
sparki.print("Serial code: ");
sparki.println(serial_code);
sparki.updateLCD();
}
}[/code]
The phone code is really, really messy. And there is a lot of it. But the core component to communicate with sparki is this C# SocketSystem class:
[code]public class SocketSystem : IDisposable
{
private DataReader _dataReader;
private DataWriter _dataWriter;
private StreamSocket _streamSocket;
public bool IsInitialized { get; private set; }
public bool IsConnected { get; private set; }
public void Initialize()
{
if (IsInitialized)
{
throw new Exception("SocketSystem already initialized");
}
IsInitialized = true;
}
public async Task ConnectAsync(HostName deviceHostName, string serviceName)
{
if (_dataReader != null)
{
_dataReader.Dispose();
}
if (_dataWriter != null)
{
_dataWriter.Dispose();
}
if (_streamSocket != null)
{
_streamSocket.Dispose();
}
_streamSocket = new StreamSocket();
await _streamSocket.ConnectAsync(deviceHostName, serviceName);
_dataReader = new DataReader(_streamSocket.InputStream);
_dataWriter = new DataWriter(_streamSocket.OutputStream);
IsConnected = true;
}
public void Dispose()
{
if (_dataReader != null)
{
_dataReader.Dispose();
_dataReader = null;
}
if (_dataWriter != null)
{
_dataWriter.Dispose();
_dataWriter = null;
}
if (_streamSocket != null)
{
_streamSocket.Dispose();
_streamSocket = null;
}
IsInitialized = false;
}
public async Task<bool> SendCommandAsync(byte[] data)
{
bool result = false;
if (_dataWriter != null)
{
_dataWriter.WriteBytes(data);
// REVIEW: results of StoreAcync() ignored
await _dataWriter.StoreAsync();
result = true;
}
return result;
}
}[/code]
To connect the Bluetooth, it requires that I the enable the device in my phone settings with Pin “0000” and in code I had to connect with deviceHostName=“ArcBotics” and serviceName=“1”.
