[SOLVED] How to import a self module into transferfunction file?


#1

Dear All,

I want to ask how to import a self-coded python module. Simple example: parameters.py that stores all the constant values.
How can I import it into a transferfunction file?

many thanks

Regards,
Zhenshan


#2

Hi Zhenshan,

You may find NRP’s resource tab helpful.

Best regards,
Luc


#3

Hi Luc,

That was great. Very appreciate for your kindly instruction.

Regards,
Zhenshan


#4

Hi Luc,

I carefully read the tutorial and try it by myself module first. it fails.
Then I just download the example SquarePathMetric.py and try to import it. Still fails.
ERROR:
No module named SquarePathMetric (Loading)

However, I am pretty sure I have uploaded it, do you have any idea about this?.


#5

Dear Zhenshan,

I have made the test and I didn’t encounter the error you reported. Could you please check that the file you uploaded to the resource folder, i.e. SquarePathMetric.py, contains the definition of the python class SquarePathMetric?
It should start like this:

from math import sqrt
from math import exp
from math import acos

class SquarePathMetric(object):
    """Class used to handle the metric of the square path benchmark."""

    class PathSegment(object):
        """
        Class used to represent one side of the square.

        It handles the performance of the robot for this segment.
        """

        # Map between pairs of quarters and the vertex in between.
        # This is used to detect when the robot has reached the
        # next segment.
        QUARTERS2VERTEX = [
            [None, 1, 0, 0],
            [1, None, 2, 1],
            [2, 2, None, 3],
            [0, 3, 3, None]
        ]

        initialized = False

Note that you should remove this definition from the benchmark_evaluation transfer function too.

Best regards,
Luc


#6

Dear Luc,

Thanks for your reply. After checking and trying again, I think I find the error for my case.
I try to import a module before the transferfunction definition and use it inside a function. It doesn’t work. Then I import it inside a function and it can be called.
Truly appreciate for your effort and patience.

Just put a picture for other people who may encounter this.

import parameters as p


#7

Hi Zhenshan,

It may be worthwhile to share the full problematic use case. So far, I am able to import modules both from outside and from within the body of a transfer function.

Best regards,
Luc


#8

Hi Luc,

As you can see in the two figures, the first one import from outside, when I hit apply, there is no error. But when I run the simulation, it gives me the error.
For the second figure, it works fine when the simulation is running.

Regards,
Zhenshan