Control quadricopter remote API vs c++

Typically: "How do I... ", "How can I... " questions
Post Reply
Robert
Posts: 6
Joined: 19 Aug 2015, 11:14

Control quadricopter remote API vs c++

Post by Robert » 19 Aug 2015, 14:44

Hello,
I'm new to v-rep and I'm on to control a quadricopter with remote API from visual studio c++ with keyboard butttons, but i'm stuck at the beggining. I've set up the client side like shown here, but i have no idea how should I do the server side. I mean is the server side ( server) in the v-rep, as a childScript, or it is another c++ project?, cause I saw in the bubbleRob there is a client and a server project too, but there is some setup in v-rep also.
If the c++ server project needed also, what should be in it and what should be in the client side? And in the quadricopter case there are also more childScripts, 1 for each propeller and the main one. Should I work with all propellers separately or only with the main childScript? Do I need to rewrite them or jsut a part of it?
I'm really confused.

coppelia
Site Admin
Posts: 7272
Joined: 14 Dec 2012, 00:25

Re: Control quadricopter remote API vs c++

Post by coppelia » 19 Aug 2015, 20:00

Hello,

take the controlTypeExamples.ttt scene. Remove all robots except for the purple robot. Then have a look at the script attached to the purple robot. There are basically only two important lines of code:

Code: Select all

	simExtRemoteApiStart(portNb) -- this server function will automatically close again at simulation end
	result=simLaunchExecutable('bubbleRobClient',portNb.." "..leftMotor.." "..rightMotor.." "..noseSensor,0) -- set the last arg. to 1 to see the console of the launched client
The first will start a remote API server service on the port that you specify. The remote API client need to connect on that same port.
The second line will start the remote API client application, which will connect to V-REP. The source code of that application is located in programming/bubbleRobClient

Finally, the quadcopter model in V-REP is a simple example. You do not really need one script for each propeller. You can do everything in the first child script of that model. The idea is that you will apply the thrust forces yourself with simAddForceAndTorque. The particles are not really important (unless you want to influence other objects with the wind generated).

Cheers

Robert
Posts: 6
Joined: 19 Aug 2015, 11:14

Re: Control quadricopter remote API vs c++

Post by Robert » 25 Aug 2015, 13:01

I managed to connect to V-REP and I was also able to write a short program in c++ to move a bubbleRob bot with keyboard. I did this by using simxSetJointTargetVelocity and for the object parameter I used what I got from simxGetObjectHandle. The objects were these two: remoteApiControlledBubbleRobLeftMotor, remoteApiControlledBubbleRobRightMotor. Now my question is, what objects should I get at the quadcopter to be able to move it and what function do I have to use?

By the way I don't want to use any script, I want to do all from the C++ application.
Here is also my code for bubbleRob moving.

Code: Select all

while (simxGetConnectionId(clientID) != -1)
		{
			
			
			if (GetAsyncKeyState('W')){
				motorSpeeds[0] = 3.1415f;
				motorSpeeds[1] = 3.1415f;
			}
			else {
				if (GetAsyncKeyState('A')){
					motorSpeeds[0] = -3.1415f;
					motorSpeeds[1] = 3.1415f;
				}
				else{
					if (GetAsyncKeyState('S')){
						motorSpeeds[0] = -3.1415f;
						motorSpeeds[1] = -3.1415f;
					}
					else{
						if (GetAsyncKeyState('D')){
							motorSpeeds[0] = 3.1415f;
							motorSpeeds[1] = -3.1415f;
						}
					}
				}
			}
			
			simxSetJointTargetVelocity(clientID, leftMotor, motorSpeeds[0], simx_opmode_oneshot);
			simxSetJointTargetVelocity(clientID, rightMotor, motorSpeeds[1], simx_opmode_oneshot);
			motorSpeeds[0] = 0;
			motorSpeeds[1] = 0;
		}

coppelia
Site Admin
Posts: 7272
Joined: 14 Dec 2012, 00:25

Re: Control quadricopter remote API vs c++

Post by coppelia » 25 Aug 2015, 16:11

You can normally do all the coding from the remote API function. The only thing you need to do on the V-REP side is start a remote API server service when simulation start. Or you can use the default remote API server service that is automatically started on port 19997 when V-REP is launched.

In case of the quadricopter, you will still have to write minimal code on the V-REP side, in a child script because: the remote API functions do not allow to directly apply a force to a specific object. So the idea is that you compute that force on the remote API client side, and send it inside of a string signal. The child script will receive that string signal, decode it, and apply the force to the object. e.g.:

remote API client:

Code: Select all

simxUChar data[4*4];
int objectHandle=23;
float force[3]={0.0f,0.0f,9.81f};
((int*)data)[0]=objectHandle;
((float*)data)[1]=force[0];
((float*)data)[2]=force[1];
((float*)data)[3]=force[2];
simxSetStringSignal(clientID,'objectIdAndForce',data,simx_opmode_oneshot);
In a non-threaded child script:

Code: Select all

if (sim_call_type==sim_childscriptcall_actuation) then
	local data=simGetStringSignal('objectIdAndForce')
	if data and #data>=16 then
		local objectId=simUnpackInts(data,0,1)[1]
		local force=simUnpackFloats(data,1,3)
		simAddForceAndTorque(objectId,force,{0,0,0})
	end
end
Cheers

kemz
Posts: 6
Joined: 07 Jul 2019, 15:48

Re: Control quadricopter remote API vs c++

Post by kemz » 10 Jul 2019, 22:00

Hello,
How do I do this in python? I am sending an array with the following code ;
data = np.array([])
objectHandle=23;
force=[0.0,0.0,9.81]
data = np.append(data,objectHandle)
data = np.append(data,force[0])
data = np.append(data,force[1])
data = np.append(data,force[2])
packData = vrep.simxPackFloats(data.flatten())
if type(packData) is bytearray:
sigV = (ctypes.c_ubyte*len(packData))(*packData)
vrep.simxWriteStringStream(clientID,'objectIdAndForce', sigV, vrep.simx_opmode_oneshot);
However I don't know how to receive it in python, I used the LUA script in the previous post above but I can't seem to get it right. Lastly what does objecthandle represent?

Thank you in advance

coppelia
Site Admin
Posts: 7272
Joined: 14 Dec 2012, 00:25

Re: Control quadricopter remote API vs c++

Post by coppelia » 15 Jul 2019, 06:41

see my answer to your other post

Post Reply