Example3.cpp

controllers used in webots - K. Narioka, 09/17/2014 02:31 PM

Download (5.5 KB)

 
1
#include <iostream>
2
#include <fstream>
3

    
4
#include <stdlib.h>
5
#include <math.h>
6

    
7
#include <rsc/misc/langutils.h>
8

    
9
#include <rsb/converter/Repository.h>
10
#include <rst/converters/rci/JointAnglesConverter.h>
11
#include <rst/converters/rci/JointTorquesConverter.h>
12
#include <rst/converters/rci/JointImpedanceConverter.h>
13
#include <rst/converters/rci/PoseConverter.h>
14

    
15
#include <cca/timing/PeriodicBeat.h>
16
#include <cca/processing/all.h>
17
#include <cca/nodes/Splitter.h>
18
#include <cca/nodes/Collector.h>
19

    
20
#include <rci/dto/JointAngles.h>
21

    
22
#include "cca-oncilla/CCAOncilla.h"
23

    
24
#define TIME_STEP 10 // Webots control step in ms
25
using namespace std;
26
using namespace boost;
27

    
28
using namespace rci;
29
using namespace rci::oncilla;
30

    
31
using namespace rsb::converter;
32
using namespace rst::converters::rci;
33

    
34
using namespace cca;
35
using namespace cca::oncilla;
36

    
37
int main() {
38

    
39
    // Create a general beat for our system
40
    BeatPtr heartbeat = PeriodicBeat::create(TIME_STEP);
41

    
42
    // Instantiate the Oncilla representation in CCA
43
    CCAOncilla oncilla = CCAOncilla(heartbeat, PortConfiguration::CCALocal);
44

    
45
    // We enable the world supervisor
46
    SupervisorWorldPtr supervworld = oncilla.getSupervisorWorld();
47
    supervworld->requestTransition(ComponentState::EXECUTION());
48
    printf("%s", supervworld->print().c_str());
49

    
50
    // We enable the trunk supervisor
51
    SupervisorTrunkPtr supervtrunk = oncilla.getSupervisorTrunk();
52
    supervtrunk->configureOutputPort("out_pose",
53
            PortConfiguration::REMOTE("/oncilla/status/pose/trunk"));
54
    supervtrunk->requestTransition(ComponentState::EXECUTION());
55
    printf("%s", supervtrunk->print().c_str());
56

    
57
    // We enable the supervisors for the feet positions
58

    
59
    SupervisorL4Ptr supervlf = oncilla.getSupervisorL4(LEFT_FORE);
60
    supervlf->configureOutputPort("out_forces", PortConfiguration::REMOTE("/oncilla/supervisor/force/lf"));
61
    printf("%s", supervlf->print().c_str());
62
    SupervisorL4Ptr supervrf = oncilla.getSupervisorL4(RIGHT_FORE);
63
    supervrf->configureOutputPort("out_forces", PortConfiguration::REMOTE("/oncilla/supervisor/force/rf"));
64
    printf("%s", supervrf->print().c_str());
65
    SupervisorL4Ptr supervlh = oncilla.getSupervisorL4(LEFT_HIND);
66
    supervlh->configureOutputPort("out_forces", PortConfiguration::REMOTE("/oncilla/supervisor/force/lh"));
67
    printf("%s", supervlh->print().c_str());
68
    SupervisorL4Ptr supervrh = oncilla.getSupervisorL4(RIGHT_HIND);
69
    supervrh->configureOutputPort("out_forces", PortConfiguration::REMOTE("/oncilla/supervisor/force/hr"));
70
    printf("%s", supervrh->print().c_str());
71

    
72
 
73
//    SupervisorL4Ptr supervrh = oncilla.getSupervisorL4(RIGHT_HIND);
74
// supervrh->configureOutputPort("out_forces",
75
//             PortConfiguration::LOCAL("/oncilla/test2"));
76
//     printf("%s", supervlf->print().c_str());
77

    
78
    // We set up a splitter to get all eight commands and pass them to the joints
79
    NodePtr anglessplit = Splitter<JointAngles>::create(8);
80
    anglessplit->setProcessingStrategy(PortTriggered::port("in"));
81
    anglessplit->configureInputPort("in",
82
            PortConfiguration::REMOTE("/oncilla/cmd/pos/all"));
83
    anglessplit->configureOutputPort("out0",
84
            PortConfiguration::LOCAL("/oncilla/cmd/pos/lf/l1"));
85
    anglessplit->configureOutputPort("out1",
86
            PortConfiguration::LOCAL("/oncilla/cmd/pos/rf/l1"));
87
    anglessplit->configureOutputPort("out2",
88
            PortConfiguration::LOCAL("/oncilla/cmd/pos/lh/l1"));
89
    anglessplit->configureOutputPort("out3",
90
            PortConfiguration::LOCAL("/oncilla/cmd/pos/rh/l1"));
91
    anglessplit->configureOutputPort("out4",
92
            PortConfiguration::LOCAL("/oncilla/cmd/pos/lf/l2"));
93
    anglessplit->configureOutputPort("out5",
94
            PortConfiguration::LOCAL("/oncilla/cmd/pos/rf/l2"));
95
    anglessplit->configureOutputPort("out6",
96
            PortConfiguration::LOCAL("/oncilla/cmd/pos/lh/l2"));
97
    anglessplit->configureOutputPort("out7",
98
            PortConfiguration::LOCAL("/oncilla/cmd/pos/rh/l2"));
99

    
100
    // We set up a splitter to get all eight commands and pass them to the joints
101
    NodePtr anglescollect = Collector<JointAngles>::create(8);
102
    anglescollect->setProcessingStrategy(Moderate::timeout(2));
103
    anglescollect->configureInputPort("in0",
104
            PortConfiguration::LOCAL("/oncilla/status/pos/lf/l1"));
105
    anglescollect->configureInputPort("in1",
106
            PortConfiguration::LOCAL("/oncilla/status/pos/rf/l1"));
107
    anglescollect->configureInputPort("in2",
108
            PortConfiguration::LOCAL("/oncilla/status/pos/lh/l1"));
109
    anglescollect->configureInputPort("in3",
110
            PortConfiguration::LOCAL("/oncilla/status/pos/rh/l1"));
111
    anglescollect->configureInputPort("in4",
112
            PortConfiguration::LOCAL("/oncilla/status/pos/lf/l2"));
113
    anglescollect->configureInputPort("in5",
114
            PortConfiguration::LOCAL("/oncilla/status/pos/rf/l2"));
115
    anglescollect->configureInputPort("in6",
116
            PortConfiguration::LOCAL("/oncilla/status/pos/lh/l2"));
117
    anglescollect->configureInputPort("in7",
118
            PortConfiguration::LOCAL("/oncilla/status/pos/rh/l2"));
119
    anglescollect->configureOutputPort("out",
120
            PortConfiguration::REMOTE("/oncilla/status/pos/all"));
121

    
122
    heartbeat->registerReceiver(anglessplit);
123
    heartbeat->registerReceiver(anglescollect);
124

    
125
    printf("*** Note:\n");
126
    printf("*** This example opens API Level 2 of the simulator for remote\n");
127
    printf("*** communication! Use the RSB logger to see what it is sending\n");
128
    printf("*** and start a remote application to move the robot.\n");
129

    
130
    heartbeat->run();
131

    
132
    return EXIT_SUCCESS;
133
}