source: proiecte/SIMEO/Simeo/src/SimeoEngine/SimpleAgent.cpp @ 167

Last change on this file since 167 was 167, checked in by (none), 14 years ago

Simeo: added final project and also older proof of concept code.

We used Git for version control, so look at the Git repo
in SIMEO/Simeo/ for more info.

File size: 2.4 KB
Line 
1#include <cstdlib>
2#include <assert.h>
3#include "SimpleAgent.h"
4
5SimpleAgent::SimpleAgent(vector<Sensor *> sensorList, vector<Actuator *> actuatorList, vector<CommChannel *> channelList)
6: Agent(sensorList, actuatorList, channelList),
7m_sensorLeft(NULL),
8m_sensorRight(NULL),
9m_sensorUp(NULL),
10m_sensorDown(NULL),
11m_actuatorLeft(NULL),
12m_actuatorRight(NULL),
13m_actuatorUp(NULL),
14m_actuatorDown(NULL),
15m_channelLeft(NULL),
16m_channelRight(NULL),
17m_channelUp(NULL),
18m_channelDown(NULL),
19m_timeStep(0),
20m_timeWave(0)
21{
22        int i;
23
24        for (i = 0; i < (int) sensorList.size(); i++)
25        {
26                Sensor *sensor = sensorList[i];
27                switch (sensor->getData()->orient)
28                {
29                case LEFT:
30                        m_sensorLeft = sensor;
31                        break;
32                case RIGHT:
33                        m_sensorRight = sensor;
34                        break;
35                case UP:
36                        m_sensorUp = sensor;
37                        break;
38                case DOWN:
39                        m_sensorDown = sensor;
40                        break;
41                }
42        }
43
44        for (i = 0; i < (int) actuatorList.size(); i++)
45        {
46                Actuator *actuator = actuatorList[i];
47                switch (actuator->getData()->orient)
48                {
49                case LEFT:
50                        m_actuatorLeft = actuator;
51                        break;
52                case RIGHT:
53                        m_actuatorRight = actuator;
54                        break;
55                case UP:
56                        m_actuatorUp = actuator;
57                        break;
58                case DOWN:
59                        m_actuatorDown = actuator;
60                        break;
61                }
62        }
63
64        for (i = 0; i < (int) channelList.size(); i++)
65        {
66                CommChannel *channel = channelList[i];
67                switch (channel->getOrient())
68                {
69                case LEFT:
70                        m_channelLeft = channel;
71                        break;
72                case RIGHT:
73                        m_channelRight = channel;
74                        break;
75                case UP:
76                        m_channelUp = channel;
77                        break;
78                case DOWN:
79                        m_channelDown = channel;
80                        break;
81                }
82        }
83}
84
85SimpleAgent::~SimpleAgent()
86{
87}
88
89void SimpleAgent::step()
90{
91        int rank;
92
93        MPI_Comm_rank(MPI_COMM_WORLD, &rank);
94
95        if (m_actuatorLeft) {
96                float p = (float) (PI * sin(PI / 4 * 2 * local - m_timeStep * PI / 100));
97                m_actuatorLeft->setTargetAngle(p);
98        }
99        if (m_actuatorRight) {
100                float p = (float) (PI * sin(PI / 4 * (2 * local + 1) - m_timeStep * PI / 100));
101                m_actuatorRight->setTargetAngle(p);
102        }
103        if (m_actuatorUp) {
104                m_actuatorUp->setTargetAngle(0);
105        }
106        if (m_actuatorDown) {
107                m_actuatorDown->setTargetAngle(0);
108        }
109
110        int x = 0;
111
112        if(!m_channelLeft) {
113                if(!m_timeStep) x = 1;
114        } else if(m_channelLeft->getChannel()->available()) {
115                m_channelLeft->getChannel()->read(&x, MPI_INT, 1);
116//              printf("[%d] received %d\n", rank, x);
117                m_timeWave = m_timeStep;
118                local = x;
119        }
120        if(m_channelRight && x) {
121//              printf("[%d] sent %d\n", rank, x);
122                x++;
123                m_channelRight->getChannel()->write(&x, MPI_INT, 1);
124        }
125
126        m_timeStep++;
127}
Note: See TracBrowser for help on using the repository browser.