Access Input / Output signals of robot out of C#

Forum Forums Visual Components 4.0 .NET Addon Programming Access Input / Output signals of robot out of C#

Visual Components Essentials has been officially released, download link

Be sure to read the Forum Rules and Guidelines before start using the Forum.

This topic contains 6 replies, has 2 voices, and was last updated by ccamilo ccamilo 2 weeks, 4 days ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #3421

    Ruedi
    Participant

    Does anyone have a little example how to access input and output signals of a robot with c#? I am able to read and write I/Os of the interface IBooleanSignal. (Like the StartStop of an conveyor) But the robots I/Os are IBooleanSignalMap. How can I access the value of an IBooleanSignalMap?

    Thank you!

    #3423
    ccamilo
    ccamilo
    Participant

    To get the IBooleanSignal defined in a port of a IBooleanSignalMap you can do something like this:

    //example: robotSignalMap is your IBooleanSignalMap

    int testPort = 101; // the port you want to check the signal value
    var portIndex = robotSignalMap.GetPortIndex(testPort);
    if(portIndex != -1) // make sure port has a signal in it
    {
    var internalSignal = robotSignalMap.GetSignal(portIndex) as IBooleanSignalMap;
    var value = internalSignal.Value;
    }

    #3426

    Ruedi
    Participant

    Thank you for your replay. I tested it like this:

    var sensorSignal = e.Component.Behaviors.Where(b => b.Type == BehaviorType.BooleanSignalMap);

    foreach (IBooleanSignalMap ibs in sensorSignal)
    {
    if (ibs.Name == “Inputs”)
    {
    int testPort = 1; // the port you want to check the signal value
    var portIndex = ibs.GetPortIndex(testPort);
    if (portIndex != -1) // make sure port has a signal in it
    {
    var internalSignal = ibs.GetSignal(portIndex) as IBooleanSignal;
    var value = internalSignal.Value;
    }
    }
    }

    The problem is: portIndex  is everytime -1. Am I right: the testPort  is the number of the signal? For example Input 1 = testPort = 1? Also IBooleanSignalMap doesn’t have a Value member. (internalSignal) Is it right to use here a IBooleanSignal?

    #3433
    ccamilo
    ccamilo
    Participant

    Hello,

    The IBooleanSignalMap  is composed by ports. Ports will have signals only if you connect them to other signals OR if you manually assign a signal to them.

    If you look at the following image. You will see that the robot has 2 signal maps (input/ouputs) with 4079 ports each. However, because they are not connected to any thing there are not signals on them.

    In the next image, one of the ports is connected to another signal in other component. When you do a connection, the port (10) creates a signal (number 0) and connect that signal to the other end.

    If you want to get the signal and its value for that port you will have to do this:

    var portIndex = robotSignalMap.GetPortIndex(10); // this will return you 0, unless you have other connections
    var internalSignal = robotSignalMap.GetSignal(portIndex) as IBooleanSignalMap;
    var value = internalSignal.Value;

    #3435
    ccamilo
    ccamilo
    Participant

    By the way, I made a mistake in my first example (as you noticed):

    var internalSignal = robotSignalMap.GetSignal(portIndex) as IBooleanSignalMap;

    should be:

    var internalSignal = robotSignalMap.GetSignal(portIndex) as IBooleanSignal;

    If you want to get the value of ALL signals in the map you can do this;

    for (int i = 0; i < robotSignalMap.PortCount; i++)
    {
    var portIndex = robotSignalMap.GetPortIndex(i);
    if (portIndex != -1) // make sure port has a signal in it
    {
    var internalSignal = robotSignalMap.GetSignal(portIndex) as IBooleanSignal;
    var value = internalSignal.Value;
    }
    }

    #3450

    Ruedi
    Participant

    Yeah, that’s true, only when I connect a signal is (portIndex != -1) = true. But “var internalSignal = robotSignalMap.GetSignal(portIndex) as IBooleanSignal;” throws anyway a NullReferenceException.

    Am I doing anything else wrong?

    ISimComponent comp = IoC.Get<ISelectionManager>().GetSelected<ISimComponent>();
    if (comp != null && comp != cashedComp)
    {

    var sensorSignal = comp.Behaviors.Where(b => b.Type == BehaviorType.BooleanSignalMap);

    foreach (IBooleanSignalMap ibs in sensorSignal)
    {
    if (ibs.Name == “Outputs”)
    {
    for (int i = 0; i < ibs.PortCount; i++)
    {
    var portIndex = ibs.GetPortIndex(i);
    if (portIndex != -1) // make sure port has a signal in it
    {
    var internalSignal = ibs.GetSignal(portIndex) as IBooleanSignal;
    var value = internalSignal.Value;
    }
    }
    }
    }
    }

    portIndex is evertime 0, also, when I connect Output 10.

    I am also wondering why  it’s not enough to define the action of a signal like this:

    Because when i program a robot in VC I can also use this output and I think they use the same methode, don’t they?

     

    #3459
    ccamilo
    ccamilo
    Participant

    Hello,

    I verified the issue. For some reason you cannot cast to IBooleanSignal, but you can just leave it as ISignal

    ISignal internalSignal = ibs.GetSignal(portIndex);

    and later cast the value

    bool value = (bool)internalSignal.Value;

     

    When you use the “Action Configuration”, you are just mapping the ouputs to action defined in the ActionScript. If your robot triggers an output and there was no signal in that output port, the map will trigger a generic signal from that port which will be caught by the Action script, otherwise it will use the signal in that port to trigger the event.

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.