
####  Core ESP global methods -- not usually modified  #####

module ESP

  def self.missingAxes (exception, *axes)
    Log.recordException exception
    Log.record("MISSING "<<(axes.collect {|a| a.name.to_s}.join ' and ') )
  end
  
  def self.configAxes axes
  #configure all (present) axes
    axes.each {|a|
      begin
        a.coast if a.respond_to? :coast  #to stop all servos before reconfig
        a.configure
        Axis.add a  #add axis to 'present' list if config succeeds
      rescue Axis::Missing
      rescue Msg::NodeOffline => offline
        missingAxes offline, a
      end
    }
  end

  def self.reconfigure axes=AxisKernel.present
  #configure all (present) axes
    Solenoid::Valves.reconfigureAll  #reconfigure present valve controllers
    configAxes axes
    Valve.settle
  end
   
  def self.configure
    Axis.removeAll
    Solenoid::Valves.configureAll  #configure all defined valve controllers
    closeValves
    configAxes AxisKernel.defined
    Valve.settle
  end
  
  def self.closeValves
    for valve in [Intake, Exhaust]
      valve.goal=:close      
    end
    begin
      Valve.settle
    rescue =>err
      Log.recordException err
    end 
  end

  def self.stop
  #emergency stop
    closeValves
    err = nil
    Axis.each{|a|
      begin
        a.stop if a.respond_to? :stop
      rescue =>err
        Log.recordException err  #stop all axes, even if exceptions occur
      end
    }
    raise err if err  #raise the last err if one occurfed
  end

  def self.asIRBtext
    AxisKernel.asIRBtext
  end

end

def stop
  ESP.stop
end
