0
Answered

Using c# script to control logic

haoyu yi (Hari) 1 month ago in General Questions updated by Support 1 month ago 2

Hello,

I'm currently using c# scripts to control the logic of my scenes, but I'm having some problems.

My question is for the PLCOutputBool and PLCInputBool signal classes how the values change when the state changes.


For example, the PLCOutputBool class contains the Status property and the Value property, which in turn contains some value properties:

public StatusBool Status;

public struct StatusBool
{
public bool Connected;
public bool ValueOverride;
public bool Value;
[HideInInspector] public bool OldValue;
}


In your example script, I noticed that the value under PLCOutputBool is modified. However, during my debugging, I found that changing the Status.ValueOverride is required to alter the signal value of my PLCOutputBool.

Can you tell me under what circumstances Value, status.Value, status.ValueOverride will change to true respectively?

here is my code 


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using game4automation;
using NaughtyAttributes;

public class PickBlank1A : ControlLogic
{
public bool PickProduct1A_On = false;


[Header("Signals")]
public PLCOutputBool StartPathPick; //初始态移动机械臂抓取
public PLCOutputBool StartPathReturn; //成功抓取后返回标准位置

public PLCOutputBool Pick;
public PLCOutputBool Place;
public PLCInputBool RobotSensorOccupied;
public PLCInputBool PathEnded;

private void Start()
{
PickProduct1A_On = false;
}

private void Update()
{
//当开启时,启动机器人路径
//Starts the robot path when turned on
if (PickProduct1A_On)
{
StartPathPick.Status.ValueOverride = true;
}
//当路径动作结束时 => 夹取传感器被占据 => 开启信号未复位时,进行夹取
//Clamping when the path action is finished => Clamping sensor is occupied => Clamping is performed when the open signal is not reset
if (PathEnded.Status.Value && RobotSensorOccupied.Status.Value && PickProduct1A_On)
{
StartPathPick.Status.ValueOverride = false; //路径信号复位 Path signal reset
//夹取 这里注意Grip夹取时使用的是Value而不是ValueOverride
Pick.Status.ValueOverride = true;
Pick.Status.Value = true;
}


//当夹取的路径走完后,判断是否夹取成功,成功夹取了再执行返回标准位置的路径
//When the clamping path is completed, determine whether the clamping is successful, successful clamping and then execute the path back to the standard position.
if (PathEnded.Status.Value == true && Pick.Status.ValueOverride == true && Pick.Status.Value == true )
{
StartPathReturn.Status.ValueOverride = true;
//返回标准位置后,该单步任务结束
//After returning to the standard position, this single-step task ends
PickProduct1A_On = false;
}
}

private void SwitchPick(PLCOutputBool pick)
{
pick.Status.ValueOverride = !pick.Status.ValueOverride;
}
}

Answered

You should always use signal.value = true or signal.value = false;

See for example the scripts in the demo model - for example this code:

Sensor Occupied is for example a PLCInputBool Signal

void FixedUpdate()
{

if (SensorOccupied.Value == false && On && ButtonConveyorOn.Value == true)
{
StartConveyor.Value = true;
}
else
{
StartConveyor.Value = false;
}

LampCanAtPosition.Value = SensorOccupied.Value;
}