Thumbnail

Single Camera Servo Server

by Synthiam

Servo camera video server which allows a remote client to move servos and receive video stream from a camera device.

Requires ARC v10 (Updated 6/14/2022)

How to add the Single Camera Servo Server robot skill

  1. Load the most recent release of ARC (Get ARC).
  2. Press the Project tab from the top menu bar in ARC.
  3. Press Add Robot Skill from the button ribbon bar in ARC.
  4. Choose the Virtual Reality category tab.
  5. Press the Single Camera Servo Server icon to add the robot skill to your project.

Don't have a robot yet?

Follow the Getting Started Guide to build a robot and use the Single Camera Servo Server robot skill.


How to use the Single Camera Servo Server robot skill

This is a servo & camera video server which allows a remote client to move servos and receive video stream from a camera device. This is specifically useful for those creating Unity apps that operate as a client to ARC. where the camera video stream can be received and servo positions can be sent. The camera must be streaming for the servo positions to transmit.

Demo #1 This is an overview of how this robot skill can integrate with a unity scene using the Unity animation tool.

Demo #2 This example uses a block in Unity that controls an EZ-Robot JD shoulder. It's a very simple example of how powerful and easy it is to control any servo in ARC. The blocks have gravity and a hinge, so as it swings the angle is read and pushed into ARC to move the respective servo.

Demo #3 This is Mickey's demonstration of controlling the servos through unity and joysticks.

How It Works Code from the client (ie unity in this case) will connect to the camera servo skill over tcp. It streams the servo positions and receives the camera view. 

User-inserted image

Example Client App Source Code Here is an example test app src that connects to localhost (127.0.0.1), moves a servo on port D2 and displays the camera video stream. The sample app is C# .Net source-code and can be downloaded.

Download C# .Net Example Source code: Test App.zip (2020/12/17)

Test Unity Project I have included a test Unity project for example reference. The example rotates a cube on the screen using the ARROW keys. The cube projects the texture from the camera stream onto it. The arrow keys will also move the servos connected to port D0 and D1 relative to the rotation of the cube.

You can download the project here: Unity Test.zip (2020/12/17)

Use In Unity The stream client files in the "Test App" can be included in a Unity project to receive the video and move servos. The test app demonstrates how to move the servos using the methods, and how to display the video on Windows. To display the video in Unity, follow the steps below. The video works by becoming a Texture2D that can be applied to any material.

To use this in your Unity App, copy the files from the Test App\ServoServerClient*.cs into your Unity project.

User-inserted image

Examine Test Project The Unity project displays the ARC camera stream on a rotating cube. While, allowing the 2d sprite to control servos D0 and D1 by the X and Y position, respectively. Clicking on the scene will move the sprite and also move the servos.

User-inserted image

Any components within the group can have their position or rotation, etc. extracted and sent to ARC. If you have a 3d model of a robot, each joint position/rotation can be sent to ARC.

The most interesting thing to look at is the Scene object -> ServoCameraGroup. Notice it has child GameObjects. Those child GameObjects can be queried for their rotation or position or whatever is desired and sent to ARC as servo positions. Also, the camera image can be rendered to any material as a 2d texture.

User-inserted image

Look at the ServoCameraGroup to see the script

User-inserted image

The script ServoCamera.cs will be responsible for Start - create and instance of the StreamClient object

  • have the StreamClient connect to ARC at an IP address (this case it's using local machine 127.0.0.1)
  • assign child gameobjects to local variables that we will be using in Update (this makes cpu happy)
  • connecte to the ARC server

Update

  • obtaining rotation/position/whatever data from children and add to the servo position cache (in this example a sprite position)
  • sending the cache of servo positions
  • displaying the incoming image on a material as a texture

Let's take a look at the code for ServoCamera.cs and read the comments of how it is working


using EZ_Robot_Unity_DLL;
using UnityEngine;

public class ServoCamera : MonoBehaviour {

  ServoServerClient _streamClient;
  bool _initialized = false;
  Texture2D _texture;

  volatile byte [] _toDisplay = new byte[]{ };

  Transform _cube;
  Transform _sprite;

  /// 
  /// We have this script added to a parent that has children. 
  /// Because we can access the children's transformation or position from here to set servo positions
  /// In the update, we'll just grab the children and use thier data to send to ARC
  /// 
  void Start() {

    // This is the texture that will hold the camera image from ARC
    // We apply this texture to a cube
    _texture = new Texture2D(640, 480, TextureFormat.RGB24, false);

    // assign a local variable to the children so we don't have to search for them on each frame (makes cpu happy)
    _cube = gameObject.transform.Find("Cube");
    _sprite = gameObject.transform.Find("MoveableSprite");

    //Create a client that will connect to ARC at the specified IP address
    // Once connected, any available video data from the ARC camera will raise the OnImageDataReady event
    _streamClient = new ServoServerClient();
    _streamClient.OnImageDataReady += _streamClient_OnImageDataReady;
    _streamClient.Start("127.0.0.1", 8282);
  }

  /// 
  /// This event is raised for every camera image that is received from the connected ARC server.
  /// We assign the image data to a volatile array that will be used in the update the texture with the latest image
  /// 
  private void _streamClient_OnImageDataReady(byte[] imageData) {

    if (!_initialized)
      return;

    _toDisplay = imageData;
  }

  void OnDisable() {

    _streamClient.Stop();
  }

  /// 
  /// Unity runs this on every render frame
  /// We get the keyboard input to move the camera around
  /// And we map the cube's X and Y rotation values to D0 and D1 servo positions in ARC, respectively
  /// 
  void Update() {

    _initialized = true;

    if (Input.GetKey(KeyCode.Escape))
      Application.Quit();

    // add the position of the servos to the cache based on the location of the sprice
    // We set the positions to cache in this loop rather than trying to send a position each time
    // That way we can send a bulk change which is much faster on bandwidth
    // So, add your servo positions to the cache and then send them all after
    _streamClient.SetCachedServoPosition(ServoServerClient.ServoPortEnum.D0, _streamClient.MapToByte(_sprite.transform.position.x));
    _streamClient.SetCachedServoPosition(ServoServerClient.ServoPortEnum.D1, _streamClient.MapToByte(_sprite.transform.position.y));

    // Send all the servo positions that have been cached
    _streamClient.SendCachedServoPositions();

    // Display the latest camera image by rendering it to the texture and applying to the cube's material
    if (_toDisplay.Length > 0) {

      _texture.LoadImage(_toDisplay);

      var material = _cube.GetComponent().material;
      material.mainTexture = _texture;
      material.mainTextureScale = new Vector2(1, -1);
    }
  }
}


ARC Pro

Upgrade to ARC Pro

Harnessing the power of ARC Pro, your robot can be more than just a simple automated machine.

#113  

Yeah, totally...its kind of a fresh approach to get stuff done, and its a big plus for a visually oriented person like myself! I don't mind coding, but a node based GUI is just such a nice thing to get started, right?

The one thing I really don't get yet is...what are you actually struggling with? The above graph shows, that you are already sending data out to the Arduino on a serial port using Unreal, so if you are using PWM servos, all you need is a servo shield and you are done!

I am using Unity for quiet a while now to send servo positions live to ARC, and it works great!

So there are three different scenarios, which are all equally legit, when using any software...

  1. sending the data directly to the serial port
  2. sending the data to the servo And Camera Server
  3. sending the data to a server of choice

Although its does not matter which Game Engine you are using for the first approach, you will loose the ability to integrate the servo positions within ARC, since ARC cannot "see" them, as they are being send to the serial port directly...

The second approach can an the moment only be done in Unity, since DJ Sures implemented his fast and reliable streaming server, that is not using http requests, but a compact binary stream for data transfer!

The third approach can also be done in both Game engines but requires a bit of extra setup. It is the route that I am currently using, so I can confirm it is working great, no lag no jitter!! Just install Flask use it to send the data over to ARC! This way you are not loosing your Game Engine of choice, and you can also have ARC to process all the servo data

I got a bit caught up in making my controller app visually more appealing as you see above...but am hoping to show it in a few days, so you can see what I am talking about!!

I am pretty sure we can get this to work for your robot!:)

PRO
USA
#114   — Edited

@mickey...are you using flask? I'd be interested in see a video with that set up...I'm as visual learner too.

Unreal is a super massive download at 25 Gigs!!!...so also looking into free Blender as another option.....much smaller foot print, this could solve some of my issues with a gaming engine. Blender is a proper animation program so has built in IK, motion blending, motion controls etc. Its all modules coded in python. So just need to find a way to get data out of Blender to flask to Arc?!

#115  

OMG, you got me on that one...Blender!!! This is some delicate subject, since I would strongly advocate for Blender. I would tell everyone to use it, to start on this platform right away! Its open source, its powerful and Python is integrated in the very core of its engine... Its runs on all mayor operating systems, Windows, Mac OS and Linux. And it is free, like open source free...free to the bare bone of it!!!

But I spend so much time learning software, that I just did not dare to walk down that road yet! Its kind of a hard transition most of the times, and I heard that the interface does not make it easier...

BUT...If you are going down that road, am super curious how things will be working out for you! A positive feedback could also be a push towards that direction for myself!!

And yes sending data out from Blender should be super easy!!:)

#116  

Finally got to film and cut a demo...There is still some fine tuning to be done regarding the IK, its a bit harder to control with a PS4 controller than in VR. But all in all it works quiet well! The bit of jitter you see comes more from structural build integrity of the robot, than from the servos itself!

I will make a short description on how I connect to ARC in a separate thread, but its not difficult at all!:)

PRO
Synthiam
#117  

Oh that’s great! Super awesome actually!:) amazing how talented you guys are. When I was playing in unity, there wasn’t much I could do. I’m pretty terrible at design lol

PRO
USA
#118   — Edited

Nice job! So is this demoing pushing to flask? Because this is really smooth motion....what is different?

I spent most of last night downloading Blender and looking around...coming from a Lightwave back ground...once you know one animation program, you kinda know them all. Since you come from Max universe should be simple to master. Most tools are there...just finding them is the trick with new software. I'll let you know my progress.

#119   — Edited

Thanks, and yes the current version is using Flask and ARC, so its all tried and tested to work out like shown in the demo... The only thing is, that I did not test it on any other servo setup, since ZOE is the only fully assembled robot in the house, and this setup is being run on LX16a Serial Bus Servos. But I guess any type of servo setup should work. Its all kinda the same thing!:)

I also tried to get my setup done using the servo And Camera Server, which will most likely work as good or better. But there is the minor annoyance that this would take more than the one allowed plugin available in my free subscription plan, because I need the Servo And Camera Server & LX16a plugin to get this to work...maybe there will be a developer subscription plan for ARC at some point in the future?

But its all working as it should, by using the Script that I have written in ARC to get the data from the server, so all is working out great with ARC free...I will post a tutorial on how to set it up soon!:)

PRO
USA
#120  

...I look forward to that tutorial. That will help me better understand your process, so I can think about a blender application.