Ejemplo n.º 1
0
bool AppGoalTension::setup()
{
    // First create the world
    world = createWorld();

    // Second create the view
    if (use_graphics)
        view = createGraphicsView(world); // For visual experimenting on one tensegrity
    else
        view = createView(world);         // For running multiple episodes

    // Third create the simulation
    simulation = new tgSimulation(*view);

    // Fourth create the models with their controllers and add the models to the
    // simulation
#if (0)
    startAngle = ((rand() / (double)RAND_MAX) - 0.5) * 3.1415;
#endif
        FlemonsSpineModelGoal* myModel =
      new FlemonsSpineModelGoal(nSegments, goalAngle, startAngle);

    // Fifth create the controllers, attach to model
    if (add_controller)
    {
        Json::Value root; // will contains the root value after parsing.
        Json::Reader reader;
        
        std::string resourcePath = "bmirletz/TC_Tension/";
        std::string controlFilePath = FileHelpers::getResourcePath(resourcePath);
        std::string controlFilename = controlFilePath + suffix;
        
        bool parsingSuccessful = reader.parse( FileHelpers::getFileString(controlFilename.c_str()), root );
        if ( !parsingSuccessful )
        {
            // report to the user the failure and their locations in the document.
            std::cout << "Failed to parse configuration\n"
                << reader.getFormattedErrorMessages();
            throw std::invalid_argument("Bad filename for JSON");
        }
        // Get the value of the member of root named 'encoding', return 'UTF-8' if there is no
        // such member.
        Json::Value impedenceVals = root.get("impedenceVals", "UTF-8");
        impedenceVals = impedenceVals.get("params", "UTF-8");
        
        // Keep drilling if necessary
        if (impedenceVals[0].isArray())
        {
            impedenceVals = impedenceVals[0];
        }
        
        const double impedanceMax = 2000.0;
        
        const int segmentSpan = 3;
        const int numMuscles = 8;
        const int numParams = 2;
        const int segNumber = 5; // For learning results
        const double controlTime = .01;
        const double lowPhase = -1 * M_PI;
        const double highPhase = M_PI;
        const double lowAmplitude = 0.0;
        const double highAmplitude = 300.0;
        // JSONCPP's .get really wants this to be typed...
        int j = 0;
        const double kt = impedanceMax * (impedenceVals.get(j, 0.0)).asDouble();
        const double kp = impedanceMax * (impedenceVals.get(1, 0.0)).asDouble();
        const double kv = impedanceMax * (impedenceVals.get(2, 0.0)).asDouble();
        const bool def = true;
            
        // Overridden by def being true
        const double cl = 10.0;
        const double lf = 0.0;
        const double hf = 30.0;
        
        // Feedback parameters
        const double ffMin = -10.0;
        const double ffMax = 10.0;
        const double afMin = 0.0;
        const double afMax = 200.0;
        const double pfMin = 0.0;
        const double pfMax =  0.0;
        const double tensionFeedback = impedanceMax *(impedenceVals.get(3, 0.0)).asDouble();

        JSONGoalControl::Config control_config(segmentSpan, 
                                                    numMuscles,
                                                    numMuscles,
                                                    numParams, 
                                                    segNumber, 
                                                    controlTime,
                                                    lowAmplitude,
                                                    highAmplitude,
                                                    lowPhase,
                                                    highPhase,
                                                    kt,
                                                    kp,
                                                    kv,
                                                    def,
                                                    cl,
                                                    lf,
                                                    hf,
                                                    ffMin,
                                                    ffMax,
                                                    afMin,
                                                    afMax,
                                                    pfMin,
                                                    pfMax,
                                                    tensionFeedback
                                                    );
        
        /// @todo fix memory leak that occurs here
        JSONGoalTension* const myControl =
        new JSONGoalTension(control_config, suffix, resourcePath);
        
        myModel->attach(myControl);
    }

    // Sixth add model & controller to simulation
    simulation->addModel(myModel);
    
    if (add_blocks)
    {
        tgModel* blockField = getBlocks();
        simulation->addObstacle(blockField);
    }
    
    bSetup = true;
    return bSetup;
}
Ejemplo n.º 2
0
bool AppGoalOnline::setup()
{
    // First create the world
    world = createWorld();

    // Second create the view
    if (use_graphics)
        view = createGraphicsView(world); // For visual experimenting on one tensegrity
    else
        view = createView(world);         // For running multiple episodes

    // Third create the simulation
    simulation = new tgSimulation(*view);

    // Fourth create the models with their controllers and add the models to the
    // simulation
    /// @todo add position and angle to configuration
    /// @todo Generalize angle code
        FlemonsSpineModelGoal* myModel =
      new FlemonsSpineModelGoal(nSegments, 0.0);

    // Fifth create the controllers, attach to model
    if (add_controller)
    {
        const int segmentSpan = 3;
        const int numMuscles = 8;
        const int numParams = 2;
        const int segNumber = 0; // For learning results
        const double controlTime = .01;
        const double lowPhase = -1 * M_PI;
        const double highPhase = M_PI;
        const double lowAmplitude = 0.0;
        const double highAmplitude = 300.0;
        const double kt = 0.0;
        const double kp = 1000.0;
        const double kv = 200.0;
        const bool def = true;
            
        // Overridden by def being true
        const double cl = 10.0;
        const double lf = 0.0;
        const double hf = 30.0;
        
        // Feedback parameters
        const double ffMin = -0.5;
        const double ffMax = 10.0;
        const double afMin = 0.0;
        const double afMax = 200.0;
        const double pfMin = -0.5;
        const double pfMax =  6.28;
        const double tensionFeedback = 1000.0;
        
        // How often to check/change the controller during online learning
        const double feedbackTime = 3.0;

        SpineOnlineControl::Config control_config(segmentSpan, 
                                                    numMuscles,
                                                    numMuscles,
                                                    numParams, 
                                                    segNumber, 
                                                    controlTime,
                                                    lowAmplitude,
                                                    highAmplitude,
                                                    lowPhase,
                                                    highPhase,
                                                    kt,
                                                    kp,
                                                    kv,
                                                    def,
                                                    cl,
                                                    lf,
                                                    hf,
                                                    ffMin,
                                                    ffMax,
                                                    afMin,
                                                    afMax,
                                                    pfMin,
                                                    pfMax,
                                                    tensionFeedback,
                                                    feedbackTime
                                                    );
        /// @todo fix memory leak that occurs here
        SpineOnlineControl* const myControl =
        new SpineOnlineControl(control_config, suffix, "bmirletz/TetrahedralComplex_Online/");

        myModel->attach(myControl);
    }

    // Sixth add model & controller to simulation
    simulation->addModel(myModel);
    
    if (add_blocks)
    {
        tgModel* blockField = getBlocks();
        simulation->addObstacle(blockField);
    }
    
    bSetup = true;
    return bSetup;
}