Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functions to allow C# control of purple robot in controlTypeExamples.ttt #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

neuronz
Copy link

@neuronz neuronz commented Nov 20, 2013

This is the first time I've used GitHub so please let me know if all is ok.
I've also created a c# version of the bubbleRobClent to run these new functions against if you're interested?

@neuronz
Copy link
Author

neuronz commented Nov 21, 2013

Here is the C# code that can be used to to replace the C++ remote bubbleRob client for the purple robot in the v-rep controlTypeExamples.ttt scene. It should be built as a C# Console application with a Platform target of x86 (and of course referencing remoteApiNETWrapper) and launched (in place of the existing C++ bubbleRobClient exe) from the purple robot's remoteApiControlledBubbleRob child script.

+++
// Copyright 2006-2013 Dr. Marc Andreas Freese. All rights reserved.
// [email protected]
// www.coppeliarobotics.com
//
// -------------------------------------------------------------------
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// You are free to use/modify/distribute this file for whatever purpose!
// -------------------------------------------------------------------
//
// This file [is a C# version (by Bruce Bayley) of the file that] was automatically created for V-REP release V3.0.4 on July 8th 2013

// Make sure to have the server side running in V-REP!
// Start the server from a child script with following command:
// simExtRemoteApiStart(portNumber) -- starts a remote API server service on the specified port

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using remoteApiNETWrapper;

namespace bubbleRobClientCS
{
class CClient
{
static int Main( string[ ] args )
{
Console.WriteLine( "bubbleRobClientCS is running...\n" );
int nArgs = args.Length;

        int portNb = 0;
        int leftMotorHandle;
        int rightMotorHandle;
        int sensorHandle;


        if( nArgs >= 4 )
        {
            portNb = int.Parse( args[ 0 ] );
            leftMotorHandle = int.Parse( args[ 1 ] );
            rightMotorHandle = int.Parse( args[ 2 ] );
            sensorHandle = int.Parse( args[ 3 ] );
        }
        else
        {
            Console.WriteLine( "Indicate following arguments: 'portNumber leftMotorHandle rightMotorHandle sensorHandle'!\n" );
            Thread.Sleep( 5000 );
            return 0;
        }

        int clientID = VREPWrapper.simwStart( "127.0.0.1", portNb, true, true, 2000, 5 );
        if( clientID != -1 )
        {
            int driveBackStartTime = -99000;
            float[ ] motorSpeeds = new float[ 2 ];

            while( VREPWrapper.simwGetConnectionId( clientID ) != -1 )
            {
                char sensorTrigger = '\0';
                int objectHandle = 0;

                simx_error error = VREPWrapper.simwReadProximitySensor( clientID, sensorHandle, ref sensorTrigger, null, ref objectHandle, null, simx_opmode.continuous );
                if( error == simx_error.noerror )
                { // We succeeded at reading the proximity sensor
                    int simulationTime = VREPWrapper.simwGetLastCmdTime( clientID );
                    int diff = simulationTime - driveBackStartTime;
                    if( diff < 3000 )
                    { // driving backwards while slightly turning:
                        motorSpeeds[ 0 ] = -3.1415f * 0.5f;
                        motorSpeeds[ 1 ] = -3.1415f * 0.25f;
                    }
                    else
                    { // going forward:
                        motorSpeeds[ 0 ] = 3.1415f;
                        motorSpeeds[ 1 ] = 3.1415f;
                        if (sensorTrigger != 0)
                            driveBackStartTime = simulationTime; // We detected something, and start the backward mode
                    }
                    VREPWrapper.simwSetJointTargetVelocity( clientID, leftMotorHandle, motorSpeeds[ 0 ], simx_opmode.oneshot );
                    VREPWrapper.simwSetJointTargetVelocity( clientID, rightMotorHandle, motorSpeeds[ 1 ], simx_opmode.oneshot );
                }
                Thread.Sleep( 5 );
            }
            VREPWrapper.simwFinish( clientID );
        }
        else
            Console.WriteLine( "simwStart failed!" );
        Thread.Sleep( 5000 );
        return ( 0 );
    }
}

}

+++

@neuronz neuronz closed this Nov 21, 2013
@neuronz neuronz reopened this Nov 21, 2013
@HWiese1980
Copy link
Owner

Well, you could implement simw wrapper functions for all the imported simx functions, however my idea for the simw wrappers was to simplify the interface. You can still invoke the simx functions directly, thus, no simw functions are really necessary for the user of the wrapper library to be able to call the V-REP functions.

Sorry to say so, but I'd rather not merge this pull request in its current state, as long as the simw functions do nothing more than encapsulating the call to the corresponding simx function. The reason is, once the library is starting to be used by others, it will be much more difficult to change the signature of the simw functions without the possibility of breaking code compatibility.

If you implement the simw functions as simplifications (return the desired value instead of using a ref or an IntPtr and throw an exception in case of an error instead of returning the error), I'll happily merge it. I'm going to rework my own simw wrapper functions for that purpose, too.

Regardless of these requirements I totally appreciate your work and effort, thanks a lot! Keep it up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants