Skip to content

Instantly share code, notes, and snippets.

@bderose
Last active October 27, 2019 21:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bderose/bc33ab34440b56f67a03aa0ea1030355 to your computer and use it in GitHub Desktop.
Save bderose/bc33ab34440b56f67a03aa0ea1030355 to your computer and use it in GitHub Desktop.
Example DNaaS API calls and workflow in C#
using System;
using System.IO;
using System.Net.Http;
using System.Text;
using Newtonsoft.Json;
class Dnaas {
private static readonly HttpClient client = new HttpClient {
BaseAddress = new Uri("http://automation.berkeley.edu/dex-net-api/"),
};
static void Main(string[] args) {
var jsonParams = JsonConvert.SerializeObject(new {
metric = "robust_ferrari_canny",
gripper = new {
fingertip_x = 0.01,
fingertip_y = 0.01,
gripper_offset = 0.01,
palm_depth = 0.05,
width = 0.08
}
});
var jsonContent = new StringContent(jsonParams, Encoding.UTF8, "application/json");
var meshContent = new ByteArrayContent(File.ReadAllBytes("/path/to/your/mesh.obj"));
var form = new MultipartFormDataContent();
form.Add(meshContent, "file", "file");
form.Add(jsonContent, "params", "params");
var response = client.PostAsync("upload-mesh", form)
.GetAwaiter()
.GetResult();
response.EnsureSuccessStatusCode();
var result = response.Content.ReadAsStringAsync().Result;
Console.WriteLine(result);
}
}
@DiegoVil
Copy link

DiegoVil commented Oct 25, 2019

Do you have any C# example on how to grab "/dex-net-api/<MESH_ID>/processing-progress"..

This is my code not working

       ```
   var response = await client.GetAsync(<ID>+"/processing-progress");

            response.EnsureSuccessStatusCode();
            var result = response.Content.ReadAsStringAsync().Result;
            return result.ToString();
       ```

EDIT: In fact is working but always in ERROR state

image

Here http://automation.berkeley.edu/dex-net/ works correctly

@wderose
Copy link

wderose commented Oct 26, 2019

@DiegoVil, can you provide a complete reproducible example of your bug? Are you making a POST call to the upload-mesh endpoint prior to polling the processing-progress endpoint?

Are you able to reproduce the error with the script below?

using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using Newtonsoft.Json;

public class Dnaas
{
    private static readonly HttpClient client = new HttpClient
    {
        BaseAddress = new Uri("http://automation.berkeley.edu/dex-net-api/"),
    };

    public static void Main(string[] args)
    {
        var jsonParams = JsonConvert.SerializeObject(new
        {
            metric = "robust_ferrari_canny",
            gripper = new
            {
                fingertip_x = 0.01,
                fingertip_y = 0.01,
                gripper_offset = 0.01,
                palm_depth = 0.05,
                width = 0.08
            }
        });
        var jsonContent = new StringContent(jsonParams, Encoding.UTF8, "application/json");
        using (WebClient webClient = new WebClient())
        {
            string s = webClient.DownloadString("http://automation.berkeley.edu/dex-net/assets/pawn/pawn.obj");
            var bytes = System.Text.Encoding.UTF8.GetBytes(s);
            var meshContent = new ByteArrayContent(bytes);
            var form = new MultipartFormDataContent();

            form.Add(meshContent, "file", "file");
            form.Add(jsonContent, "params", "params");
            var uploadResponse = client.PostAsync("upload-mesh", form)
                .GetAwaiter()
                .GetResult();

            uploadResponse.EnsureSuccessStatusCode();
            var result = uploadResponse.Content.ReadAsStringAsync().Result;
            var anonResponse = new
            {
                id = string.Empty,
                position = -1
            };
            var deserializedResp = JsonConvert.DeserializeAnonymousType(result, anonResponse);
            Console.WriteLine(deserializedResp);
            var state = "preprocessing";
            while (state != "error" || state != "done")
            {
                var progressResponse = client.GetAsync(deserializedResp.id + "/processing-progress")
                    .GetAwaiter()
                    .GetResult();
                progressResponse.EnsureSuccessStatusCode();
                var progress = progressResponse.Content.ReadAsStringAsync().Result;
                Console.WriteLine(progress);
                System.Threading.Thread.Sleep(5000);
            }
        }
    }
}

I'm using .Net Core, with the following .csproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
  </ItemGroup>

</Project>

My output:

{ id = 08bd9a72-3c1c-4464-bb6a-7ee055f5a399, position = 1 }
{
  "state": "preprocessing"
}

{
  "state": "sampling grasps"
}

{
  "state": "sampling grasps"
}

{
  "percent done": 0.24912280701754386, 
  "state": "collision checking for stable poses"
}

{
  "percent done": 0.9157894736842106, 
  "state": "collision checking for stable poses"
}

{
  "percent done": 0.16842105263157894, 
  "state": "computing metrics"
}

{
  "percent done": 0.3684210526315789, 
  "state": "computing metrics"
}

{
  "percent done": 0.5684210526315789, 
  "state": "computing metrics"
}

{
  "percent done": 0.7368421052631579, 
  "state": "computing metrics"
}

{
  "percent done": 0.9368421052631579, 
  "state": "computing metrics"
}

{
  "state": "done"
}
wderose@LM-SJN-21021416 foo $ dotnet run
{ id = a6d88d63-c937-44ed-9170-7d2f43d71c4d, position = 1 }
{
  "state": "preprocessing"
}

{
  "state": "sampling grasps"
}

{
  "state": "sampling grasps"
}

{
  "percent done": 0.11024305555555555, 
  "state": "collision checking for stable poses"
}

{
  "percent done": 0.8324652777777778, 
  "state": "collision checking for stable poses"
}

{
  "percent done": 0.15625, 
  "state": "computing metrics"
}

{
  "percent done": 0.3333333333333333, 
  "state": "computing metrics"
}

{
  "percent done": 0.5208333333333334, 
  "state": "computing metrics"
}

{
  "percent done": 0.7083333333333334, 
  "state": "computing metrics"
}

{
  "percent done": 0.8958333333333334, 
  "state": "computing metrics"
}

{
  "state": "done"
}

@DiegoVil
Copy link

@wderose
This is my class... I was doing just a quick test...I have a windows form with 2 buttons, with the first button I upload from my computer and I get an ID from my mesh. Then with second button and a textbox in windows form I add " my commands " to see if it works...
So, my post function returns id and then I send the command with a get

` class Dnaas
    {
        static string id = "";
        private static readonly HttpClient client = new HttpClient
        {
            BaseAddress = new Uri("http://automation.berkeley.edu/dex-net-api/"),
        };

        static public async System.Threading.Tasks.Task<string> GetAsync(string text)
        {
            // Use static HttpClient to avoid exhausting system resources for network connections.

            var response = await client.GetAsync(text);
                
            response.EnsureSuccessStatusCode();
            var result = response.Content.ReadAsStringAsync().Result;

            return result.ToString();
            // Write status code.
          //  Console.WriteLine("STATUS CODE: " + response.StatusCode);
            
           
        }

        static public string  PostGeometry()
        {
            var jsonParams = JsonConvert.SerializeObject(new
            {
                metric = "robust_ferrari_canny",
                gripper = new
                {
                    fingertip_x = 0.01,
                    fingertip_y = 0.01,
                    gripper_offset = 0.01,
                    palm_depth = 0.05,
                    width = 0.08
                }
            });
            var jsonContent = new StringContent(jsonParams, Encoding.UTF8, "application/json");
            var meshContent = new ByteArrayContent(File.ReadAllBytes("C:\\myadress\\Leap.obj"));
            var form = new MultipartFormDataContent();

            form.Add(meshContent, "file", "file");
            form.Add(jsonContent, "params", "params");
            var response = client.PostAsync("upload-mesh", form)
                .GetAwaiter()
                .GetResult();

            response.EnsureSuccessStatusCode();
            var result = response.Content.ReadAsStringAsync().Result;
            return result.ToString();
            //GET /dex-net-api/<MESH_ID>/stable-poses
            Console.WriteLine(result);


        }
    }
    }`

image

@DiegoVil
Copy link

DiegoVil commented Oct 26, 2019

This is with your example and sample, same error...maybe I am doing something wrong..I am using this:
<?xml version="1.0" encoding="utf-8"?> <configuration> <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/></startup></configuration>

image

static public void PostFromweb()
        {
           var jsonParams = JsonConvert.SerializeObject(new
            {
                metric = "robust_ferrari_canny",
                gripper = new
                {
                    fingertip_x = 0.01,
                    fingertip_y = 0.01,
                    gripper_offset = 0.01,
                    palm_depth = 0.05,
                    width = 0.08
                }
            });
            var jsonContent = new StringContent(jsonParams, Encoding.UTF8, "application/json");
            using (WebClient webClient = new WebClient())
            {
                string s = webClient.DownloadString("http://automation.berkeley.edu/dex-net/assets/pawn/pawn.obj");
                var bytes = System.Text.Encoding.UTF8.GetBytes(s);
                var meshContent = new ByteArrayContent(bytes);
                var form = new MultipartFormDataContent();

                form.Add(meshContent, "file", "file");
                form.Add(jsonContent, "params", "params");
                var uploadResponse = client.PostAsync("upload-mesh", form)
                    .GetAwaiter()
                    .GetResult();

                uploadResponse.EnsureSuccessStatusCode();
                var result = uploadResponse.Content.ReadAsStringAsync().Result;
                var anonResponse = new
                {
                    id = string.Empty,
                    position = -1
                };
                var deserializedResp = JsonConvert.DeserializeAnonymousType(result, anonResponse);
                Console.WriteLine(deserializedResp);
                var state = "preprocessing";
                while (state != "error" || state != "done")
                {
                    var progressResponse = client.GetAsync(deserializedResp.id + "/processing-progress")
                        .GetAwaiter()
                        .GetResult();
                    progressResponse.EnsureSuccessStatusCode();
                    var progress = progressResponse.Content.ReadAsStringAsync().Result;
                    Console.WriteLine(progress);
                    System.Threading.Thread.Sleep(5000);
                }
            }



        }

EDIT: After several clicks.... I have this output:
{
"state": "sampling grasps"
}

{
"percent done": 0.13,
"state": "collision checking"
}

The thread 0x2368 has exited with code 0 (0x0).
{
"percent done": 0.3175438596491228,
"state": "collision checking for stable poses"
}

{
"percent done": 0.010526315789473684,
"state": "computing metrics"
}

{
"percent done": 0.22105263157894736,
"state": "computing metrics"
}

{
"percent done": 0.4,
"state": "computing metrics"
}

{
"percent done": 0.6210526315789474,
"state": "computing metrics"
}

{
"percent done": 0.8421052631578947,
"state": "computing metrics"
}

{
"state": "done"
}

{
"state": "done"
}

{
"state": "done"
}
EDIT 2:

Adding this lines to your code works perfectly!
BTW: Beautiful project

 while (state != "error" || state != "done")
                {
                    var progressResponse = client.GetAsync(deserializedResp.id + "/processing-progress")
                        .GetAwaiter()
                        .GetResult();
                    progressResponse.EnsureSuccessStatusCode();
                    var progress = progressResponse.Content.ReadAsStringAsync().Result;
                    Console.WriteLine(progress);
                    if (progress.Contains("\"state\": \"done\""))
                    {
                 
                        break;
                    }
                    System.Threading.Thread.Sleep(5000);
                }

@wderose
Copy link

wderose commented Oct 26, 2019

@DiegoVil sounds like you got things working. Glad to help.

@DiegoVil
Copy link

@wderose : Yes , its working now...

One more question: I have gone through the paper but I have a doubt.... in the poses that you calculate where is the origin ? in the center of mass of the object? how can I know where is located the basis of coordinates?

@wderose
Copy link

wderose commented Oct 27, 2019

The stable pose-associated grasp is relative to the object. We assume the mesh is watertight, so it should be the CoM of the object. In order to use grasp configuration, you may still need to apply a transformation to grasp pose. The quaternion is wxyz.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment