Ziro Logo

Home

Modules UDP Scripts Unity

Examples

Mobile: IMU Cloud: Alexa Arduino: PIXY Arduino: Sensors

Cloud based application

Unity

Developers can leverage cloud platforms that enable you to develop apps powered by cloud services such as Amazon Web Services, Google Tensorflow etc to add intelligence to the robot. In this exmaple, we show how developers can create a pipeline that allows the Ziro Robotics kit to act on commands from cloud services using a phone.

The pipeline of dataflow has two elements - (a) data from a publishing device such as Alexa Echo, going to the cloud, (b) data from the cloud coming to the subscribing device i.e. mobile phone. The phone then interprets the data prepares the UDP message packet and broadcasts to the motor modules of the Ziro kit.

We will leverage a third-party service PubNub for our publish and subscribe message mechanism. This example talks about the integration of PubNub services in our game scene for Unity. The Unity application will be running on our phone and hence would be the subscribing device, that will receive the message from the cloud. Similarly, our publishing device would be an echo dot, that will publish to the cloud. For this example, we will use Amazon Web Services (AWS) as our cloud platform.

Subscribing Device

The script can be downloaded from the Scripts section under the title of Alexa Subscriber. This application is built on top of the UDPClient.cs setup. As all examples go, this is an application which requires the UDPClient.cs to be attached to the main game object. The link to the script is here

After all the variable initialization in the code, we start the publish and subscribe channels in our Start(). More resources on how to do start the publish and subscribe channel can be found in PubNub website here

    
    void Start()
    {

    m_EulerAngleVelocity = new Vector3(0, 20, 0);
    rb = GetComponent();


    PNConfiguration pnConfiguration = new PNConfiguration();
    pnConfiguration.SubscribeKey = "sub-c-404ad2cc-1410-11e7-bb8a-xxxxxxxxxxxxx";
    pnConfiguration.PublishKey = "pub-c-92ed06dd-cde5-460f-9047-xxxxxxxxxxxxx";
    pnConfiguration.Secure = false;
    Dictionary msg = new Dictionary();

    pubnub = new PubNub(pnConfiguration);
    
    pubnub.Subscribe()
        .Channels(new List(){
            "my_channel"
        })
        .Execute();
        
    }
  

Please pay attention to the fact that the publish and subscribe key will be different for different users. For the sake of this example, we have put some imaginary values in those variables. These keys are unique to each publish-subscribe channel.

Let's jump to the latter half of the code where the SusbcribeCallback() is called. This function extracts the intent and the command from the cloud. The publishing device will publish the phrases on the cloud. We use this intent to develop our logic to send message packets via UDP.

  
    pubnub.SusbcribeCallback += (sender, e) => { 
    SusbcribeEventEventArgs mea = e as SusbcribeEventEventArgs;
          
        if (mea.MessageResult != null) {
            Debug.Log("Payload from alexa: " + mea.MessageResult.Payload.ToString());
            AlexaVal = mea.MessageResult.Payload.ToString();

            if (AlexaVal == "forward" || AlexaVal == "initiate" ){
              flag = 1;
            }
            else if (AlexaVal == "backward"){
              flag = 2;
            }
            else if (AlexaVal == "left"){
              flag = 3;
            }
            else if (AlexaVal == "right"){
              flag = 4;
            }
            else if (AlexaVal == "takeOff"){
              flag = 5;
            }
        }
    };
  

As you can see, we are associating flag based on each phrase that helps us enter conditions on what type of message needs to be sent. We use the FixedUpdate() to modify the modes and attach target values for the motors based on the flags we got from the subscribe function later.

  
    void FixedUpdate()
    { 
      Quaternion deltaRotation = Quaternion.Euler(m_EulerAngleVelocity * Time.deltaTime);

      if (flag == 1)
      {
        s_M = "[2,1,0,0,0,0,0]";
        m_T = 55;
      }
      else if (flag == 2)
      {
        s_M = "[1,2,0,0,0,0,0]";
        m_T = 55;
      }
      else if (flag == 3)
      {
          s_M = "[1,1,0,0,0,0,0]";
          m_T = 40;
      }
      else if (flag == 4)
      {
          s_M = "[2,2,0,0,0,0,0]";
          m_T = 40;
      }
      else if (flag == 5)
      {
          s_M = "[2,1,0,0,0,0,0]";
          m_T = 0;
      }

      else if (flag == 0)
      {
          s_M = "[2,1,0,0,0,0,0]";
          m_T = 0;
      }

      s_T = m_T.ToString() + "," + m_T.ToString() + ",0,0,0,0,0";
      string mssg = "{\"pType\":7,\"m_T\":[" + s_T + "],\"m_M\":" + s_M + "}";
      gameObject.GetComponent ().SendValue (mssg);

  

Publishing Device

The publishing device, in this case, is Amazon Alexa Echo. Developers can develop simple Alexa apps using Amazon Web Services. In the AWS code, we can define our PubNub credentials to publish the message. The subscribing device can then subscribe to the message and associate logic.

Since this application is based on Alexa, we developed an Alexa app. The core of any Alexa app is the Lambda function which runs on AWS. We associated our PubNub credentials to the Lambda function, where we declared our publisher credentials. As shown below in our Alexa project folder, we would have an Index.js, that would contain the credentials for the PubNub services.

  
    var pubnub = require("pubnub")({
    ssl           : true,  // <- enable TLS Tunneling over TCP 
    publish_key: "pub-c-92ed06dd-cde5-460f-9047-xxxxxxxxxxxxx",
    subscribe_key: "sub-c-404ad2cc-1410-11e7-bb8a-xxxxxxxxxxxxx"
});


Refer to this resource to set up an Alexa app with PubNub - Link