2
votes

We developed a simple V-REP simulation that works pretty well on OS X but not on Linux (Ubuntu 15.04). Lua is employed as the main scripting language.

We attach the following code (with the return values in comments) that unfortunately returns nil on Linux (but converts e[3] to number without problem on OS X):

e[3]                   -- -0.677782532263
type(e[3])             -- string
type(tonumber(e[3]))   -- nil

What is really interesting is the fact that the previous code works as one would expect in Lua 5.2.3 console (both OS X and Linux). However, V-REP can't convert the string to number properly when running on Linux.

We tried both 32b and 64b V-REP versions (today downloaded) with exactly same results - nils. Could you please point out some things we're missing? Neither Lua nor V-REP are utils we use every day.

Edit 1: I Use Ubuntu 15.04. V-REP uses Lua 5.1, My Lua version:

$ apt-cache policy lua5.1
lua5.1:
  Installed: 5.1.5-7.1
  Candidate: 5.1.5-7.1
  Version table:
 *** 5.1.5-7.1 0
        500 http://cz.archive.ubuntu.com/ubuntu/ vivid/main amd64 Packages
        100 /var/lib/dpkg/status

In console, I tried the following:

$ lua
Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
> e={[3]="-0.677782532263"}; print(e[3], type(e[3]), tonumber(e[3]), type(tonumber(e[3])))
-0.677782532263 string  -0.677782532263 number

Packages

2
Do you really get Nil printed out? Or is it nil? You are sure that you are using the same version of lua on both OS X and linux when this happens? And both of the first two lines output the same value on OS X and linux when this happens? - Etan Reisner
Hello Etan, we get nil, the version is the same on both platforms, and about the output - the values are dynamically created in python and sent via remote api to vrep (and lua), so they are not the same exact values, but the error doesn't happen on os x at all and happens on linux on all the float numbers that get passed in. - Gyfis
@EtanReisner it's exactly as Gyfis wrote. - petrbel
What do you get for the value of e[3] if you try print(("%q"):format(e[3]))? What distribution of linux is this? What lua package exactly? Can you get that value to work in lua itself (without vrep) if you try it manually? (i.e. e={[3]="-0.677782532263"}; print(e[3], type(e[3]), tonumber(e[3]))?) - Etan Reisner
@EtanReisner please see my edit 1 with answers. It seems like it works in console. - petrbel

2 Answers

4
votes

The error is caused by the fact that V-REP uses Lua 5.1, and the computers we tested it on had different locales for numbers (the linux had LC_NUMERIC=cs_CZ.UTF-8 and the mac had, probably, en_US).

That means that Lua on Mac recognized the float numbers in strings as numbers, but Lua on Linux with the different locale did not - it didn't have a comma (e.g. -3,513) as a decimal separator that the locale required, so it returned nil for the conversion.

The fix is to set the LC_NUMERIC flag before running the vrep to the en_US locale, like this:

...$ LC_NUMERIC=en_US.UTF-8 ./vrep

which would force the locale to be a dot-based, and enable Lua to recognize the numbers.

Thanks @Etan for all the help and for poking at the issue from the right direction.

0
votes

Another way to set locale from lua code like this os.setlocale("C")

Not all systems can change the locale of an arbitrary application at startup, sometimes it will be necessary to change the locale of the entire system, which can be inconvenient for other applications. If the script distributed with V-REP is distributed in the same way with XML files in which the separator is a period, rather than a comma, then it is logical that before the tonumber function is called, the correct console should be installed from LUA and not outside, through the variables of the open environment.

The LUA documentation clearly describes what the os.setLocale function is used for, and it clearly states that it affects the conversion of strings to number. And there it is indicated in the need to make sure the correct settings before calling the conversion functions.

In addition, the script that changes the locale within itself is more portable, because does not require the person who launched the knowledge of what environment variables for V-REP should be specified from the outside.