Main Page · Modules · All Classes · Class Hierarchy
MAWalkBaseBehaviors.cpp
1 /*
2  * This file is part of the AiBO+ project
3  *
4  * Copyright (C) 2005-2016 Csaba Kertész (csaba.kertesz@gmail.com)
5  *
6  * AiBO+ is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * AiBO+ is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
19  *
20  */
21 
22 #include "MAWalkBaseBehaviors.hpp"
23 
24 #include "types/MAComplexIndicators.hpp"
25 #include "types/MAGoals.hpp"
26 #include "types/MARobotState.hpp"
27 #include "MAAvoidObstacleMaster.hpp"
28 #include "MARecognizeFloorSurface.hpp"
29 #include "MAWalkBalanceToLeft.hpp"
30 #include "MAWalkBalanceToRight.hpp"
31 #include "MAWalkPushBackward.hpp"
32 #include "MAWalkPushForward.hpp"
33 #include "MAWalkSwingBackward.hpp"
34 #include "MAWalkSwingForward.hpp"
35 
36 namespace
37 {
38 bool IsWalkFinished()
39 {
40  if (MA::RobotState->ComplexIndicators->ElapsedForwardWalkTime > 0 &&
41  MA::RobotState->ComplexIndicators->NearObjectForward > 90)
42  {
43  MC_LOG("Stop walking: near object forward (%d)",
44  (int)MA::RobotState->ComplexIndicators->NearObjectForward);
46  return true;
47  }
48  if (MA::RobotState->ComplexIndicators->ElapsedForwardWalkTime > 0 &&
49  MA::RobotState->ComplexIndicators->AbyssWhileWalking > 90)
50  {
51  MC_LOG("Stop walking: abyss while walking (%d)",
52  (int)MA::RobotState->ComplexIndicators->AbyssWhileWalking);
54  return true;
55  }
56  if (MA::RobotState->Goals->LiePostureDesire > 0 || MA::RobotState->Goals->SitPostureDesire > 0 ||
57  MA::RobotState->Goals->StandPostureDesire > 0)
58  {
59  MC_LOG("Stop walking: desired posture (lie: %d, sit: %d, stand: %d)",
60  (int)MA::RobotState->Goals->LiePostureDesire, (int)MA::RobotState->Goals->SitPostureDesire,
61  (int)MA::RobotState->Goals->StandPostureDesire);
62  return true;
63  }
64  if (MA::RobotState->Goals->DesiredLeftTurnDuration > 0)
65  {
66  MC_LOG("Stop walking: to turn left");
67  return true;
68  }
69  if (MA::RobotState->Goals->DesiredRightTurnDuration > 0)
70  {
71  MC_LOG("Stop walking: to turn right");
72  return true;
73  }
74  if (MA::RobotState->ComplexIndicators->ElapsedForwardWalkTime > 0 &&
75  MA::RobotState->Goals->DesiredBackwardWalkDuration > 0)
76  {
77  MC_LOG("Stop walking: to walk backward");
78  return true;
79  }
80  if (MA::RobotState->ComplexIndicators->ElapsedBackwardWalkTime > 0 &&
81  MA::RobotState->Goals->DesiredForwardWalkDuration > 0)
82  {
83  MC_LOG("Stop walking: to walk forward");
84  return true;
85  }
86 
87  // Stop the walk movements if the elapsed walk duration is over the desired walk duration.
88  int DesiredDuration = 0;
89  int CurrentDuration = 0;
90 
91  if (MA::RobotState->Goals->DesiredForwardWalkDuration > 0 &&
92  MA::RobotState->ComplexIndicators->ElapsedForwardWalkTime > 0)
93  {
94  DesiredDuration = MA::RobotState->Goals->DesiredForwardWalkDuration;
95  CurrentDuration = MA::RobotState->ComplexIndicators->ElapsedForwardWalkTime;
96  } else
97  if (MA::RobotState->Goals->DesiredBackwardWalkDuration > 0 &&
98  MA::RobotState->ComplexIndicators->ElapsedBackwardWalkTime > 0)
99  {
100  DesiredDuration = MA::RobotState->Goals->DesiredBackwardWalkDuration;
101  CurrentDuration = MA::RobotState->ComplexIndicators->ElapsedBackwardWalkTime;
102  }
103  return CurrentDuration >= DesiredDuration;
104 }
105 }
106 
107 MAWalkLegBehaviorBase::MAWalkLegBehaviorBase(const std::string& behavior_name, MA::Leg::LegTypes leg_type) :
108  MALegBehavior(behavior_name, leg_type)
109 {
110 }
111 
112 
114 {
115  return IsWalkFinished();
116 }
117 
118 
120 {
121  // Destroy all other swing forward/push backward/balance behaviors
122  for (int i = (int)MA::Leg::LF; i <= (int)MA::Leg::RH; ++i)
123  {
124  MALEGBEHAVIOR_DELETE(MAWalkSwingForward, (MA::Leg::LegTypes)i);
125  MALEGBEHAVIOR_DELETE(MAWalkPushBackward, (MA::Leg::LegTypes)i);
126  }
127  // Destroy all other swing backward/push forward/balance behaviors
128  for (int i = (int)MA::Leg::LF; i <= (int)MA::Leg::RH; ++i)
129  {
130  MALEGBEHAVIOR_DELETE(MAWalkSwingBackward, (MA::Leg::LegTypes)i);
131  MALEGBEHAVIOR_DELETE(MAWalkPushForward, (MA::Leg::LegTypes)i);
132  }
136 }
137 
138 
139 MAWalkBehaviorBase::MAWalkBehaviorBase(const std::string& behavior_name, bool dynamic) :
140  MABehavior(behavior_name, dynamic)
141 {
142 }
143 
144 
146 {
147  return IsWalkFinished();
148 }
Balances the body to left.
#define MABEHAVIOR_DELETE(_behavior)
Delete a behavior safely (mark it dirty if exists)
Definition: MABehavior.hpp:81
Base class for leg behaviors.
MABehavior(const std::string &type_name, bool dynamic=true)
Create a behavior.
Definition: MABehavior.cpp:67
Push the leg forward.
virtual bool IsFailed() override
Whether the behavior is failed.
Swing the leg forward.
#define MABEHAVIOR_CREATE(_behavior, _master,...)
Create behaviors with custom constructor safely.
Definition: MABehavior.hpp:43
Swing the leg backward.
Avoid an abyss or an object while walking.
virtual void FailingActions() override
Perform actions when the behavior is being failed.
virtual bool IsFailed() override
Whether the behavior is failed.
Indicate the floor surface recognition period.
#define MC_LOG(...)
Debug macro.
Definition: MCLog.hpp:41
Balances the body to Right.
Push the leg backward.