Jump to content

Solar Panel Auto-Deployment/Retract


Sarahleth

Recommended Posts

Or KRPC.    Here in the new script library you can see my rover autopilot example.   It brings the rover to a stop every 5 minutes and autosaves (but DOESN'T overwrite an autosave if it detects the rover is tipped or stuck... so you always have the last 'good' save).   It would be an easy thing to add a function to check EC and do a stop and deploy until the EC was back above a threshold.   If you decide to go the KRPC route I'm happy to add that function in for you if it seems to do everything else you want. 

My experience with mechjeb's rover autopilot - if you try to apply the brakes while the autopilot is engaged it immediately disengages them and accelerates again - so any mod you want to use the way you've described is actually going to have to turn off the mechjeb autopilot and then reengage it, if you're using mechjeb to drive.  That's not a hard thing to do if you're making a mod from scratch and if you get everything else ready I'll show you how to do that with reflection.  

Is this within the realm of mechjeb's new scripting module?  I haven't played with that.  Anyway - the link to my KRPC demo folder is:

https://github.com/krpc/krpc-library/tree/master/Art_Whaleys_KRPC_Demos

Link to comment
Share on other sites

I was amused enough that I just went ahead and added this to my KRPC script.  I've got a few more things to do before I make another pull request but you can get the version on my fork at

 

https://github.com/artwhaley/krpc-library

You'll need to download the rover.py and the pid.py files and then in the same directory you'd create your script.   You don't have to understand everything in the rover or pid files to use it.   You just need to make a script that will call the functions.   As an example this script could look like:

import krpc
from rover import rover_go

conn=krpc.connect()   ## connect to KRPC
## next create a waypoint.  Replace the coordinates (0.05, -75.0,)  with the lat and lon you want to drive to
wp1 = conn.space_center.waypoint_manager.add_waypoint(  
        0.05,-75.0, conn.space_center.active_vessel.orbit.body,"Waypoint1")
rover_go(conn, wp1)  #call the rover script.   

## the rover script will then do it's thing, recharging when battery is low and autosaving every 5 minutes
## if the situation looks nominal enough.

 

Link to comment
Share on other sites

I've got a working mod that deploys when stopped. I've only got 1.2.2 installed at the moment, so I've compiled it and tested against that.

https://github.com/genericeventhandler/AutomaticLights/releases/tag/0.02

here's the source code if you want to compile it yourself. 

Spoiler

  public class DeployableSafetyModule : PartModule
    {
        private static bool isActive = true;

        private static string lastMessage;

        private System.DateTime elapsed;

        private bool isDeployed;

        [KSPField()]
        public double maxSurfaceSpeed;

        public void Update()
        {
            DoAction();
        }

        public override void OnUpdate()
        {
            DoAction();
        }

        [KSPEvent(guiActive = true, guiName = "Toggle auto deploy", active = true)]
        public void ToggleMode()
        {
            DeployableSafetyModule.isActive = !isActive;
            SendMessageToScreen("Auto deployables are " + (isActive ? " on" : "off"));
        }

        private void SendMessageToScreen(string message)
        {
            if (lastMessage != message)
            {
                ScreenMessages.PostScreenMessage(message);
                lastMessage = message;
            }
        }

        private void DoAction()
        {
            if (!isActive)
            {
                return;
            }

            var vessel = FlightGlobals.ActiveVessel;
            if (vessel == null)
            {
                return;
            }

            if (elapsed.CompareTo(System.DateTime.Now) < 0)
            {
                // only check every three seconds.
                elapsed = System.DateTime.Now.AddSeconds(3);
            }
            else
            {
                return;
            }

            switch (vessel.situation)
            {
                case Vessel.Situations.LANDED:
                case Vessel.Situations.SPLASHED:
                    DoSplashed();
                    return;

                case Vessel.Situations.SUB_ORBITAL:
                case Vessel.Situations.FLYING:
                    DoFlying();
                    return;

                case Vessel.Situations.ESCAPING:
                case Vessel.Situations.ORBITING:
                case Vessel.Situations.PRELAUNCH:
                default:
                    return;
            }
        }

        private void DoFlying()
        {
            var vessel = FlightGlobals.ActiveVessel;
            if (vessel == null)
            {
                return;
            }

            var planet = FlightGlobals.currentMainBody;
            if (planet == null)
            {
                return;
            }

            if (planet.atmosphere && vessel.dynamicPressurekPa > 1)
            {
                DoSplashed();
                return;
            }
            else
            {
                if (vessel.terrainAltitude < 1000)
                {
                    FoldPanels(vessel, false);
                }
            }

            DoSplashed();
        }

        private void DoSplashed()
        {
            var vessel = FlightGlobals.ActiveVessel;
            if (vessel == null)
            {
                return;
            }

            if (vessel.horizontalSrfSpeed > maxSurfaceSpeed && isDeployed)
            {
                FoldPanels(vessel, false);
                return;
            }

            if (vessel.horizontalSrfSpeed <= maxSurfaceSpeed && !isDeployed)
            {
                FoldPanels(vessel, true);
                return;
            }
        }

        private void FoldPanels(Vessel vessel, bool deploy)
        {
            foreach (Part p in vessel.Parts)
            {
                foreach (PartModule m in p.Modules)
                {
                    var solar = m as ModuleDeployablePart;
                    if (solar != null)
                    {
                        if (deploy && solar.deployState == ModuleDeployablePart.DeployState.RETRACTED)
                        {
                            solar.Extend();
                            SendMessageToScreen("Deploying");
                            isDeployed = true;
                            break;
                        }

                        if (!deploy && solar.deployState == ModuleDeployablePart.DeployState.EXTENDED)
                        {
                            solar.Retract();
                            SendMessageToScreen("Retracting");
                            isDeployed = false;
                            break;
                        }

                        break;
                    }
                }
            }
        }
    }

 

Edit: I've got a video comming, but youtube is taking way way too long to process it. 

 

I'm trying to figure out why the NRE is spamming from the DeployableSolarPanel.OnFixedUpdate - I'll edit this post if I correct it. 


GEH

 

 

Edited by genericeventhandler
Link to comment
Share on other sites

So you use a mod that already does 90% of your needs and instead of asking "Hey, can you add a 'retract panels when moving' option to MJ" (MJ already has all the panels deployments code since it does it for the ascent AP & scripting module) you ask for a brand new mods ?

Link to comment
Share on other sites

43 minutes ago, sarbian said:

So you use a mod that already does 90% of your needs and instead of asking "Hey, can you add a 'retract panels when moving' option to MJ" (MJ already has all the panels deployments code since it does it for the ascent AP & scripting module) you ask for a brand new mods ?

For me it's more of a thought process to learn how to do things. I know that MJ does all this, and I use smart parts a lot to automate stuff. It's just fun to figure stuff out. 

Geh. 

Link to comment
Share on other sites

24 minutes ago, genericeventhandler said:

For me it's more of a thought process to learn how to do things. I know that MJ does all this, and I use smart parts a lot to automate stuff. It's just fun to figure stuff out. 

Geh. 

It was not directed at you :)

Link to comment
Share on other sites

12 hours ago, sarbian said:

So you use a mod that already does 90% of your needs and instead of asking "Hey, can you add a 'retract panels when moving' option to MJ" (MJ already has all the panels deployments code since it does it for the ascent AP & scripting module) you ask for a brand new mods ?

Given the amount of work that you have put into MJ and other great mods, I personally would be hesitant to ask for a feature that might be a one off for only a few users.  We appreciate what you've done and would hate to bother you.  But if it's easiest for you to add such a script into MJ, that would be awesome. 

Link to comment
Share on other sites

  • 2 weeks later...
On 15/07/2017 at 11:39 PM, artwhaley said:

I was amused enough that I just went ahead and added this to my KRPC script.  I've got a few more things to do before I make another pull request but you can get the version on my fork at

 

https://github.com/artwhaley/krpc-library

You'll need to download the rover.py and the pid.py files and then in the same directory you'd create your script.   You don't have to understand everything in the rover or pid files to use it.   You just need to make a script that will call the functions.   As an example this script could look like:


import krpc
from rover import rover_go

conn=krpc.connect()   ## connect to KRPC
## next create a waypoint.  Replace the coordinates (0.05, -75.0,)  with the lat and lon you want to drive to
wp1 = conn.space_center.waypoint_manager.add_waypoint(  
        0.05,-75.0, conn.space_center.active_vessel.orbit.body,"Waypoint1")
rover_go(conn, wp1)  #call the rover script.   

## the rover script will then do it's thing, recharging when battery is low and autosaving every 5 minutes
## if the situation looks nominal enough.

 

Thanks, i'll check this out when i have the time.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...