Add Ardunity Controller

What is Ardunity Controller?

ArdunityController is user's code for ARDUINO and Unity.

1.Ardunity Controller for ARDUINO

1-1. Create C++ Class file

The source code of header file (*.h)

#ifdef MyController_h

#define MyController_h

class MyController

{

public:

MyController();

protected:

private:

}

#endif

The source code of implementation file (*.cpp)

#include "MyController.h"

MyController :: MyController()

{

}

This header and implementation files must be existed in the name of folder 'Arduino'.

(The folder path does not matter , it is only if the name 'Arduino'.)

1-2. Inheritance from ArdunityController

The source code of header file (*.h)

#ifdef MyController_h

#define MyController_h

#include "ArdunityController.h"

class MyController : public ArdunityController

{

public:

MyController(int id);

protected:

// Implementation of virtual function

void OnSetup();

void OnStart();

void OnStop();

void OnProcess();

void OnUpdate();

void OnExecute();

void OnFlush();

private:

}

#endif

The source code of implementation file (*.cpp)

#include "MyController.h"

MyController :: MyController(int id) : ArdunityController(id)

{

}

void MyController :: OnSetup()

{

// Arduino setup

}

void MyController :: OnStart()

{

// When connected to Unity

}

void MyController :: OnStop()

{

// When disconnected to Unity

}

void MyController :: OnProcess()

{

// Arduino loop

}

void MyController :: OnUpdate()

{

// When you receive data from Unity

}

void MyController :: OnExecute()

{

// Do something after receiving data

}

void MyController :: OnFlush()

{

// When you send data to Unity

}

1-3. Declare variable to share with Unity

Supported data types to communicate are as follows:

    • UINT8: 8bit unsiged integer number (0 ~ 255)

    • INT8: 8bit signed integer number (-128 ~ 127)

  • UINT16: 16bit unsiged integer number (0 ~ 65,535)

  • INT16: 16bit signed integer number (-32,768 ~ 32,767)

  • UINT32: 32bit unsiged integer number (0 ~ 4,294,967,295)

  • INT32: 32bit signed integer number (–2,147,483,648 ~ 2,147,483,647)

  • FLOAT32: 32bit floating point number

  • STRING: String (Maximum length is 255)

The source code of header file (*.h)

#ifdef MyController_h

#define MyController_h

#include "ArdunityController.h"

class MyController : public ArdunityController

{

public:

MyController(int id);

protected:

// Implementation of virtual function

void OnSetup();

void OnStart();

void OnStop();

void OnProcess();

void OnUpdate();

void OnExecute();

void OnFlush();

private:

// The variable for sharing between Arduino and Unity

UINT8 uint8_data;

INT8 int8_data;

UINT16 uint16_data;

INT16 int16_data;

UINT32 uint32_data;

INT32 int32_data;

FLOAT32 float32_data;

STRING string_data;

char string_buffer[50]; // buffer for STRING

}

#endif

1-4. Implement Constructor

The source code of implementation file (*.cpp)

MyController::MyController(int id) : ArdunityController(id)

{

string_data = stringBuffer; // STRING should connect to buffer.

canFlush = true; // If send data to Unity, must set true.

canFlush = false; // If do not send data to Unity, must set false.

}

1-5. Implement process for initializing or destroying

The source code of implementation file (*.cpp)

void MyController::OnSetup()

{

// Arduino setup

}

void MyController::OnStart()

{

// When connected to Unity

}

void MyController::OnStop()

{

// When disconnected to Unity

}

1-6. Implement process for receiving data from Unity

The source code of implementation file (*.cpp)

#include "Ardunity.h"

void MyController::OnUpdate()

{

// When you receive data from Unity

// 'Pop' means that Arduino get data from Unity

// The order is important (It must be same with push order in Unity)

ArdunityApp.pop(&uint8_data);

ArdunityApp.pop(&int8_data);

ArdunityApp.pop(&uint16_data);

ArdunityApp.pop(&int16_data);

ArdunityApp.pop(&uint32_data);

ArdunityApp.pop(&int32_data);

ArdunityApp.pop(&float32_data);

ArdunityApp.pop(string_data, 30); // STRING need to know a size of buffer

updated = true; // It must be set true to run OnExecute

}

void MyController::OnExecute()

{

// Do something after receiving data

}

1-7. Collect data to be sent to Unity

The source code of implementation file (*.cpp)

void MyController::OnProcess()

{

// Arduino loop

if(started)

{

// When connected to Unity

}

else

{

// When disconnected to Unity

dirty = true; // It must be set true to run OnFlush

}

}

1-8. Implement process for sending data to Unity

The source code of implementation file (*.cpp)

#include "Ardunity.h"

void MyController::OnFlush()

{

// When you send data to Unity

// 'Push' means that Arduino throw data to Unity

// The order is important (It must be same with pop order in Unity)

ArdunityApp.push(uint8_data);

ArdunityApp.push(int8_data);

ArdunityApp.push(uint16_data);

ArdunityApp.push(int16_data);

ArdunityApp.push(uint32_data);

ArdunityApp.push(int32_data);

ArdunityApp.push(float32_data);

ArdunityApp.push(string_data);

}

2. Ardunity Controller for Unity

2-1. Create C# script

The source code of script file (*.cs)

using UnityEngine;

using System.Collections;

public class MyController : MonoBehaviour

{

void Start()

{

}

void Update()

{

}

}

2-2. Inheritance from ArdunityController

The source code of script file (*.cs)

using UnityEngine;

using System.Collections;

using Ardunity;

public class MyController : ArdunityController

{

void Start()

{

}

void Update()

{

}

}

2-3. Declare variable to share with ARDUINO

Supported data types to communicate are as follows:

    • UINT8: 8bit unsiged integer number (0 ~ 255)

    • INT8: 8bit signed integer number (-128 ~ 127)

  • UINT16: 16bit unsiged integer number (0 ~ 65,535)

  • INT16: 16bit signed integer number (-32,768 ~ 32,767)

  • UINT32: 32bit unsiged integer number (0 ~ 4,294,967,295)

  • INT32: 32bit signed integer number (–2,147,483,648 ~ 2,147,483,647)

  • FLOAT32: 32bit floating point number

  • STRING: String (Maximum length is 255)

(You can find C# code in 'ARDUnityBasic(Deluxe)/Scripts/Internal/ControllerDataType.cs' for the above.)

The source code of script file (*.cs)

// Types of data for transferring

using INT8 = System.SByte;

using UINT8 = System.Byte;

using INT16 = System.Int16;

using UINT16 = System.UInt16;

using INT32 = System.Int32;

using UINT32 = System.UInt32;

using FLOAT32 = System.Single;

using STRING = System.String;

public class MyController : ArdunityController

{

UINT8 uint8_data;

INT8 int8_data;

UINT16 uint16_data;

INT16 int16_data;

UINT32 uint32_data;

INT32 int32_data;

FLOAT32 float32_data;

STRING string_data = "";

}

2-4. Implement process for initializing or destroying

The source code of script file (*.cs)

// If you will use Awake of MonoBehaviour, must override Awake.

protected override void Awake()

{

base.Awake();

enableUpdate = true; // If set ture then receive data automatically

enableUpdate = false; // If set false then don't receive data

}

protected override void OnConnected()

{

// When connected to Arduino

}


protected override void OnDisconnected()

{

// When disconnected to Arduino

}

protected override void OnReset()

{

// When Arduino is reset

}

2-5. Implement process for receiving data from ARDUINO

The source code of script file (*.cs)

protected override void OnPop()

{

// When you receive data from Arduino

// 'Pop' means that Unity get data from Arduino

// The order is important (It must be same with push order in Arduino)

Pop(ref uint8_data);

Pop(ref int8_data);

Pop(ref uint16_data);

Pop(ref int16_data);

Pop(ref uint32_data);

Pop(ref int32_data);

Pop(ref float32_data);

Pop(ref string_data);

updated = true; // It must be set true to run OnExecute

}

protected override void OnExecuted()

{

// Do something after receiving data

}

2-6. Collect data to be sent to ARDUINO

The source code of script file (*.cs)

void Update()

{

if(connected)

{

// When connected to Arduino

SetDirty(); // It must call to run OnPush

}

else

{

// When disconnected to Arduino

}

}

2-7. Implement process for sending data to ARDUINO

The source code of script file (*.cs)

protected override void OnPush()

{

// When you send data to Arduino

// 'Push' means that Unity throw data to Arduino

// The order is important (It must be same with pop order in Arduino)

Push(uint8_data);

Push(int8_data);

Push(uint16_data);

Push(int16_data);

Push(uint32_data);

Push(int32_data);

Push(float32_data);

Push(string_data);

}

2-8. Implement process for exporting ARDUINO Sketch

The source code of script file (*.cs)

public override string[] GetAdditionalFiles()

{

// To export Arduino sketch

// If some files are required, you can copy to sketch folder

// This file must exist in folder name of 'Arduino'

return null;

}

public override string[] GetCodeIncludes()

{

// To export Arduino sketch

// You can describe "#include" in Arduino sketch

return null;

}

public override string[] GetCodeDefines()

{

// To export Arduino sketch

// You can describe "#define" in Arduino sketch

return null;

}

public override string GetCodeDeclaration()

{

// To export Arduino sketch

// You can describe declaration in Arduino sketch

return string.Format("{0} {1}({2:d});", this.GetType().Name, GetCodeVariable(), id);

}

public override string GetCodeVariable()

{

// To export Arduino sketch

// You can describe name of declaration in Arduino sketch

return string.Format("myController{0:d}", id);

}

3. Test your Ardunity Controller

3-1. Add component

Inspector of MyController

Block of MyController in Wire Editor

3-2. Wiring

Example of wiring for MyController

3-3. Test with Arduino Board

See this link