get GyroSensor data from python - B0

Typically: "How do I... ", "How can I... " questions
Post Reply
dlmed
Posts: 10
Joined: 24 Apr 2020, 03:01

get GyroSensor data from python - B0

Post by dlmed »

Hey! I'm new with CoppeliaSim, but I've already done the connection between python and CoppeliaSim via B0. I managed to set joints velocities from python, but now what I need in order to control my robot is to get the GyroSensor data to calculate the tilt angle of my self-balancing robot.
In CoppeliaSim I have the GyroSensor with its default script attached to my robot's body. I also start the simulation and everything from python, so I think in some way I have to call the Gyro script functions, but I don't know how to do it correctly, I tried this:

client.gyro_signal = ' '
def gyro_signal_callback(msg):
....client.gyro_signal = msg[1]

client.simxCallScriptFunction('sysCall_init@GyroSensor', 1, [], client.simxServiceCall())

client.simxCallScriptFunction('sysCall_sensing@GyroSensor', 1, [], client.simxServiceCall())

gyro_signal = client.simxGetStringSignal('gyroData', client.simxDefaultSubscriber(gyro_signal_callback))

print(client.gyro_signal)

But I get None in client.gyro_signal and errors in CoppeliaSim console:

B0 Remote API: error while calling function 'CallScriptFunction': [string "CUSTOMIZATION SCRIPT b0RemoteApiServer"]:134: Script is not initialized (or has already ended) (sim.callScriptFunction)
B0 Remote API: error while calling function 'CallScriptFunction': [string "CUSTOMIZATION SCRIPT b0RemoteApiServer"]:134: Script is not initialized (or has already ended) (sim.callScriptFunction)

What should I do in order to get correctly data from GyroSensor in my python code?

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

Re: get GyroSensor data from python - B0

Post by coppelia »

Hello,

here a very simple way: in your gyro script, remove

Code: Select all

local gyroData={0,0,0}
from the sensing section. At put this in the init section:

Code: Select all

gyroData={0,0,0}
And add following function:

Code: Select all

function getData()
    return(gyroData)
end
Then you can call this function with:

Code: Select all

import b0RemoteApi
import math

with b0RemoteApi.RemoteApiClient('b0RemoteApi_pythonClient','b0RemoteApi',60) as client:    

    def data_callback(msg):
        print(msg)
        if type(msg[1])==bytes:
            msg[1]=msg[1].decode('ascii') # python2/python3 differences

    client.simxCallScriptFunction('getData@GyroSensor','sim.scripttype_childscript',None,client.simxDefaultSubscriber(data_callback));

    client.simxStartSimulation(client.simxServiceCall())

    while True:
        client.simxSpinOnce()
    client.simxStopSimulation(client.simxServiceCall())
or a similar code.

Cheers

dlmed
Posts: 10
Joined: 24 Apr 2020, 03:01

Re: get GyroSensor data from python - B0

Post by dlmed »

Thanks! with this now I can do calculations for tilt angle!

Post Reply