Main Page · Modules · All Classes · Class Hierarchy
MAEarsController.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 "MAEarsController.hpp"
23 
24 #include "core/MADevice.hpp"
25 #include "core/MARandomness.hpp"
26 #include "core/MAValueGenerators.hpp"
27 #include "types/skits/MAMotionSkit.hpp"
28 
29 #include <MCLog.hpp>
30 
31 namespace MA
32 {
34 }
35 
37 {
38  if (MA::Ears.get())
39  {
40  MC_WARNING("The global ears controller is overridden.");
41  MA::Ears.release();
42  }
43  MA::Ears.reset(this);
44  MA_BIND_TRANSITION_ACTION(MAEarsController, FlapEars);
45 }
46 
47 
49 {
50  if (MA::Ears.get() == this)
51  MA::Ears.release();
52 }
53 
54 
56 {
57  MC_UNUSED(state);
58 }
59 
60 
61 MA::DeviceList MAEarsController::GetMotors() const
62 {
63  return MA::DeviceList();
64 }
65 
66 
67 bool MAEarsController::IsMovingEars() const
68 {
69  return EarLeft->HasGenerator() || EarRight->HasGenerator();
70 }
71 
72 
74 MA::DeviceGeneratorsMap MAEarsController::FlapEars()
75 {
76  MAGeneratorContainer* EarLContainer = nullptr;
77  MAGeneratorContainer* EarRContainer = nullptr;
78  MA::DeviceGeneratorsMap DeviceGenerators;
79  MA_RANDOM_POINT_1(Flaps, 1, 3);
80  MA_RANDOM_POINT_N(FlapDurations, Flaps, 100, 200);
81 
82  if (Flaps < 1)
83  return MA::DeviceGeneratorsMap();
84  for (int i = 0; i < Flaps; ++i)
85  {
86  MAPeriodicGenerator* Generator = new MAPeriodicGenerator(0.0, 1.0, 200, 0, FlapDurations[i], 0, 100, false);
87 
88  if (!EarLContainer)
89  {
90  EarLContainer = new MAGeneratorContainer(*Generator);
91  } else {
92  EarLContainer->AddGenerator(*Generator);
93  }
94  Generator = new MAPeriodicGenerator(0.0, 1.0, 200, 0, FlapDurations[i], 0, 100, false);
95  if (!EarRContainer)
96  {
97  EarRContainer = new MAGeneratorContainer(*Generator);
98  } else {
99  EarRContainer->AddGenerator(*Generator);
100  }
101  }
102  EarLeft->AddGeneratorContainer(*EarLContainer);
103  MA_INSERT_DEVICEGENERATOR(DeviceGenerators, EarLeft.get(), EarLContainer);
104  EarRight->AddGeneratorContainer(*EarRContainer);
105  MA_INSERT_DEVICEGENERATOR(DeviceGenerators, EarRight.get(), EarRContainer);
106  return DeviceGenerators;
107 }
109 
110 
111 MA::DeviceGeneratorsMap MAEarsController::GetSkitTransitionGenerators(MA::SkitBaseSPtr skit)
112 {
113  MAMotionSkit* MotionSkit = dynamic_cast<MAMotionSkit*>(skit.get());
114 
115  if (!MotionSkit)
116  return MA::DeviceGeneratorsMap();
117 
118  MA::DeviceGeneratorsMap DeviceGenerators;
119  MAGeneratorContainer* Container = nullptr;
120 
121  // Ear left
122  Container = MotionSkit->GetGeneratorContainer(MA::EarLeft);
123  EarLeft->ClearGenerators();
124  EarLeft->AddGeneratorContainer(*Container);
125  MA_INSERT_DEVICEGENERATOR(DeviceGenerators, EarLeft.get(), Container);
126  // Ear right
127  Container = MotionSkit->GetGeneratorContainer(MA::EarRight);
128  EarRight->ClearGenerators();
129  EarRight->AddGeneratorContainer(*Container);
130  MA_INSERT_DEVICEGENERATOR(DeviceGenerators, EarRight.get(), Container);
131  return DeviceGenerators;
132 }
#define MA_RANDOM_POINT_1(_variable_name, _min, _max)
Set a random point with one value.
void AddGenerator(MAGeneratorBase &generator, bool in_front=false)
Add a new generator after the last internal generator.
boost::shared_ptr< MADevice > EarRight
Ear right.
virtual MA::DeviceList GetMotors() const override
Get the list of the motors.
#define MC_WARNING(...)
Warning macro.
Definition: MCLog.hpp:43
Base class for the controllers.
virtual ~MAEarsController()
Class destructor.
boost::shared_ptr< MADevice > EarLeft
Ear left.
MAEarsController()
Class constructor.
#define MC_UNUSED(a)
Helper macro to avoid compiler warning about unused function parameters.
Definition: MCDefs.hpp:601
virtual void UpdateRobotState(MARobotState &state) override
Update the robot state.
Generator container class.
MAGeneratorContainer * GetGeneratorContainer(MA::Leg::LegTypes leg, MA::LegJointsType joint) const
Get generator container for a leg joint.
virtual MA::DeviceGeneratorsMap GetSkitTransitionGenerators(MA::SkitBaseSPtr skit) override
Get skit transition generators.
Motion sequence (motion skit) class.
A wrapper class to cover boost::thread_specific_ptr/folly::ThreadLocal API on certain targets...
Periodic generator class (waveform _/‾\_)
Robot state.
Ears controller class.
#define MA_RANDOM_POINT_N(_variable_name, _count, _min, _max)
Set a random point with a value list.