Welcome to Keen Software House Forums! Log in or Sign up to interact with the KSH community.
  1. You are currently browsing our forum as a guest. Create your own forum account to access all forum functionality.

Trying to program a sun tracking solar panel.

Discussion in 'Gameplay Help' started by Jakejud, Jun 5, 2017.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. Jakejud Apprentice Engineer

    Messages:
    114
    SOLVED
    So this is the first time i have tried out the programming block, and I'm having a rather frustrating time.

    Every time I try and run the program I get this
    "Object reference not set to an instance of an object."

    From what i gather this means the program can not find the physical objects on the grid (please correct me if i"m wrong).
    I have been re-naming and rechecking over and over to see any spelling mistakes, I'm pretty sure there are none.

    I also made a second timer and program block to see if it could find a single object on the grid, it could not.
    I'm assuming my game is not broken, and then I'm just making some stupid mistake, that can pass the compile, but can not run.
    I expect that I'm missing a something somewhere, but i can not find any information that is helpfull online.

    Btw i have NO programming experience, so please speak to me like you are speaking to a child lol.
    Thanks for any help in advance.


    Basically copies from this youtube video



    EDIT : Removed code. working on it, going to post new code soon i think i figured out some issues.
    EDIT:
    Got it working, just a few random case sensitive errors. Learned a bunch though, trying to debug it. Here is the new code


    bool SetupComplete = false;

    IMyTimerBlock timer;
    IMyMotorAdvancedStator pan;
    IMyMotorAdvancedStator tilt;
    IMySolarPanel up;
    IMySolarPanel down;
    IMySolarPanel left;
    IMySolarPanel right;
    bool pan_Clockwise = true;
    bool tilt_Clockwise = false;
    float speed = 0.25f;
    float nullZone = 0.0005f;
    float stopSpeed = 0f;
    string timerCon;
    string panCon;
    string tiltCon;
    string upCon;
    string downCon;
    string leftCon;
    string rightCon;

    public void Main(string argument)
    {
    if (!SetupComplete) Setup();

    Track();

    timer.ApplyAction("Start");
    }

    void Setup()
    {
    IMyTextPanel display = GridTerminalSystem.GetBlockWithName("LCD Solar") as IMyTextPanel;
    if (display == null)
    {
    Echo("No Display found");
    return;
    }
    display.WritePublicText("Display Found");
    display.ShowPublicTextOnScreen();

    timer = GridTerminalSystem.GetBlockWithName("Solar Panel Timer") as IMyTimerBlock;
    if (timer == null)
    {
    timerCon = ("no timer");
    Echo("No timer found");
    return;
    }
    else
    {
    timerCon = ("Timer found");
    }

    pan = GridTerminalSystem.GetBlockWithName("Advanced Rotor Pan") as IMyMotorAdvancedStator;
    if (pan == null)
    {
    panCon = ("No pan rotor found");
    Echo("No timer found");
    return;
    }
    else
    {
    panCon = ("Pan rotor found");
    }

    tilt = GridTerminalSystem.GetBlockWithName("Advanced Rotor Tilt") as IMyMotorAdvancedStator;
    if (tilt == null)
    {
    tiltCon = ("No tilt rotor found");
    Echo("No timer found");
    return;
    }
    else
    {
    tiltCon = ("Tilt rotor found");
    }

    up = GridTerminalSystem.GetBlockWithName("Solar Panel Up") as IMySolarPanel;
    if (up == null)
    {
    upCon = ("No Up panel found");
    Echo("No timer found");
    return;
    }
    else
    {
    upCon = ("Up panel found");
    }

    down = GridTerminalSystem.GetBlockWithName("Solar Panel Down") as IMySolarPanel;
    if (down == null)
    {
    downCon = ("No down panel found");
    Echo("No Up panel found");
    return;
    }
    else
    {
    downCon = ("Down panel found");
    }

    left = GridTerminalSystem.GetBlockWithName("Solar Panel Left") as IMySolarPanel;
    if (left == null)
    {
    leftCon = ("No left panel found");
    Echo("No left panel found");
    return;
    }
    else
    {
    leftCon = ("Left panel found");
    }

    right = GridTerminalSystem.GetBlockWithName("Solar Panel Right") as IMySolarPanel;
    if (right == null)
    {
    rightCon = ("No right panel found");
    Echo("No right panel found");
    return;
    }
    else
    {
    rightCon = ("Right panel found");
    }
    display.WritePublicText(timerCon +'\n'+ panCon +'\n'+ tiltCon +'\n'+ upCon +'\n'+ downCon +'\n'+ leftCon +'\n'+ rightCon);
    SetupComplete = true;
    Echo("SetupClear");
    }

    void Track()
    {
    //Pan Tracking
    Echo("Tracking Start");

    if (pan_Clockwise)
    {
    Echo("checking R/L CW input");
    if (right.MaxOutput > left.MaxOutput + nullZone)
    {
    Echo("RotaionVelocity +");
    pan.SetValue("Velocity", speed * -1);
    }
    else if (left.MaxOutput > right.MaxOutput + nullZone)
    {
    Echo("Rotation Velocity -");
    pan.SetValue("Velocity", speed);
    }
    else
    {
    Echo("RotaionVelocity 0");
    pan.SetValue("Velocity", stopSpeed);
    }
    Echo("Bugcheck1");
    }
    Echo("Bugcheck2");
    if (!pan_Clockwise)
    {
    Echo("checking R/L CCW input");
    if (right.MaxOutput > left.MaxOutput + nullZone)
    {
    Echo("RotaionVelocity +");
    pan.SetValue("Velocity", speed);
    }
    else if (left.MaxOutput > right.MaxOutput + nullZone)
    {
    Echo("RotaionVelocity -");
    pan.SetValue("Velocity", speed * -1);
    }
    else
    {
    Echo("RotaionVelocity 0");
    pan.SetValue("Velocity", stopSpeed);
    }
    }

    //Tilt Tracking

    if (tilt_Clockwise)
    {
    if (up.MaxOutput > down.MaxOutput + nullZone)
    {
    tilt.SetValue("Velocity", speed * -1);
    }
    else if (down.MaxOutput > up.MaxOutput + nullZone)
    {
    tilt.SetValue("Velocity", speed);
    }
    else
    {
    tilt.SetValue("Velocity", stopSpeed);
    }
    }
    if (!tilt_Clockwise)
    {
    if (up.MaxOutput > down.MaxOutput + nullZone)
    {
    tilt.SetValue("Velocity", speed);
    }
    else if (down.MaxOutput > up.MaxOutput + nullZone)
    {
    tilt.SetValue("Velocity", speed * -1);
    }
    else
    {
    tilt.SetValue("Velocity", stopSpeed);
    }
    }
    Echo("tracking end");
    }
     
    Last edited: Jun 6, 2017
Thread Status:
This last post in this thread was made more than 31 days old.