22 #include "MAHeadController.hpp" 24 #include "core/MACameraDevice.hpp" 25 #include "core/MADevice.hpp" 26 #include "core/MAMicrophoneDevice.hpp" 27 #include "core/MARandomness.hpp" 28 #include "types/skits/MAMotionSkit.hpp" 29 #include "types/MAAudio.hpp" 30 #include "types/MAHead.hpp" 31 #include "types/MARobotState.hpp" 33 #include <MCContainers.hpp> 41 MAHeadTransitionGenerators::MAHeadTransitionGenerators() : NeckTilt1(nullptr), NeckTilt2(nullptr),
47 void MAHeadTransitionGenerators::AddGeneratorToNeckTilt1(
MAGeneratorBase& generator)
53 NeckTilt1->AddGenerator(generator);
58 void MAHeadTransitionGenerators::AddGeneratorToNeckTilt2(
MAGeneratorBase& generator)
64 NeckTilt2->AddGenerator(generator);
69 void MAHeadTransitionGenerators::AddGeneratorToNeckPan(
MAGeneratorBase& generator)
75 NeckPan->AddGenerator(generator);
80 void MAHeadTransitionGenerators::AddDelayToNeckJoints(
int duration)
88 void MAHeadTransitionGenerators::Normalize()
90 if (!NeckTilt1 && !NeckTilt2 && !NeckPan)
94 if ((NeckTilt1 && !NeckTilt1->IsFinite()) ||
95 (NeckTilt2 && !NeckTilt2->IsFinite()) ||
96 (NeckPan && !NeckPan->IsFinite()))
103 if (NeckTilt1 && NeckTilt1->IsFinite())
104 Duration = Duration > NeckTilt1->GetDuration() ? Duration : NeckTilt1->GetDuration();
105 if (NeckTilt2 && NeckTilt2->IsFinite())
106 Duration = Duration > NeckTilt2->GetDuration() ? Duration : NeckTilt2->GetDuration();
107 if (NeckPan && NeckPan->IsFinite())
108 Duration = Duration > NeckPan->GetDuration() ? Duration : NeckPan->GetDuration();
112 Ret = Ret && (!NeckTilt1 || NeckTilt1->GetDuration() == Duration);
113 Ret = Ret && (!NeckTilt2 || NeckTilt2->GetDuration() == Duration);
114 Ret = Ret && (!NeckPan || NeckPan->GetDuration() == Duration);
121 if (NeckTilt1 && NeckTilt1->IsFinite())
123 AddGeneratorToNeckTilt1(*
new MADelayGenerator(Duration-NeckTilt1->GetDuration()));
125 if (NeckTilt2 && NeckTilt2->IsFinite())
127 AddGeneratorToNeckTilt2(*
new MADelayGenerator(Duration-NeckTilt2->GetDuration()));
129 if (NeckPan && NeckPan->IsFinite())
131 AddGeneratorToNeckPan(*
new MADelayGenerator(Duration-NeckPan->GetDuration()));
141 MC_WARNING(
"The global head controller is overridden.");
144 MA::Head.reset(
this);
146 MA_BIND_TRANSITION(
MAHeadController, TurnDownAndAhead, IsTurnedDownAndAhead);
166 if (MA::Head.
get() ==
this)
179 state.
Head->CameraBrightness =
Camera->GetBrightness();
180 state.
Head->EyePupilSize =
Camera->GetPupilSize();
181 state.
Head->LeftRight = (int)(
float)LeftRightTurned.
Value;
188 state.
Audio->LeftMicrophonePower = (int)
Microphone->GetLeftChannelPower();
189 state.
Audio->RightMicrophonePower = (int)
Microphone->GetRightChannelPower();
190 state.
Audio->BackgroundSoundPower = (int)
Microphone->GetBackgroundSoundPower();
191 const MC::IntList& Votes =
Microphone->GetSoundEventVotes();
193 for (
int i = 1; i < (int)Votes.size() && i < MA::MaxSoundEvents+1; ++i)
195 state.
Audio->SoundEventVotes[i] = Votes[i];
203 MA::DeviceList Motors;
207 Motors.push_back(
NeckPan.get());
212 MADevice* MAHeadController::GetMotor(
const MA::HeadJointsType joint)
const 214 if (joint == MA::NeckPan)
218 if (joint == MA::NeckTilt1)
226 MA::HeadJointsType MAHeadController::GetMotorType(
const MADevice& motor)
const 234 return MA::NeckTilt1;
236 return MA::NeckTilt2;
240 void MAHeadController::SetPower(
bool new_state)
245 SetPower(MA::NeckPan, new_state);
246 SetPower(MA::NeckTilt1, new_state);
247 SetPower(MA::NeckTilt2, new_state);
251 bool MAHeadController::IsPowerOn()
const 253 return IsPowerOn(MA::NeckPan) && IsPowerOn(MA::NeckTilt1) && IsPowerOn(MA::NeckTilt2);
257 bool MAHeadController::IsMovingNeck()
const 263 bool MAHeadController::IsTurnedDown()
const 273 bool MAHeadController::IsTurnedAhead()
const 277 Ret = Ret &&
MCAbs((
int)LeftRightTurned.
Value) < 30;
282 bool MAHeadController::IsUp()
const 292 MA::DeviceGeneratorsMap MAHeadController::TurnDownAndAhead()
298 LeftRight.
Value = 0.0;
301 CreateGenerators(Generators, LeftRight, 1500);
302 CreateGenerators(Generators, Position, 1500);
303 return ApplyGenerators(Generators,
true);
307 bool MAHeadController::IsTurnedDownAndAhead()
const 309 return IsTurnedDown() && IsTurnedAhead();
313 MA::DeviceGeneratorsMap MAHeadController::TurnUp()
319 LeftRight.
Value = 0.0;
322 CreateGenerators(Generators, LeftRight, 1500);
323 CreateGenerators(Generators, Position, 1500);
324 return ApplyGenerators(Generators,
true);
328 bool MAHeadController::IsTurnedUp()
const 332 Ret = Ret &&
MCAbs((
int)LeftRightTurned.
Value) < 30;
339 MA::DeviceGeneratorsMap MAHeadController::ToNormalAhead()
345 LeftRight.
Value = 0.0;
348 CreateGenerators(Generators, LeftRight, 1500);
349 CreateGenerators(Generators, Position, 1500);
351 Generators.AddDelayToNeckJoints();
352 return ApplyGenerators(Generators,
true);
356 bool MAHeadController::IsNormalAhead()
const 360 Ret = Ret &&
MCAbs((
int)LeftRightTurned.
Value) < 10;
367 MA::DeviceGeneratorsMap MAHeadController::GoRubbing()
379 CreateGenerators(Generators, NewPosition, 700);
380 return ApplyGenerators(Generators,
false);
384 MA::DeviceGeneratorsMap MAHeadController::RubToLeft()
396 CreateGenerators(Generators, NewPosition, 400);
397 Generators.Normalize();
399 CreateGenerators(Generators, NewPosition, 200+Speed1*10);
401 CreateGenerators(Generators, NewPosition, 300+Speed3*10);
403 CreateGenerators(Generators, NewPosition, 200+Speed2*10);
405 CreateGenerators(Generators, NewPosition, 300+Speed4*10);
407 CreateGenerators(Generators, NewPosition, 400);
410 NewLeftRight.
Value -= 15;
411 CreateGenerators(Generators, NewLeftRight, 200+Speed1*10);
412 NewLeftRight.
Value += 15;
413 CreateGenerators(Generators, NewLeftRight, 300+Speed3*10);
414 NewLeftRight.
Value -= 15;
415 CreateGenerators(Generators, NewLeftRight, 200+Speed1*10);
416 NewLeftRight.
Value += 15;
417 CreateGenerators(Generators, NewLeftRight, 300+Speed4*10);
418 return ApplyGenerators(Generators,
false);
422 MA::DeviceGeneratorsMap MAHeadController::RubToRight()
434 CreateGenerators(Generators, NewPosition, 200+Speed1*10);
436 CreateGenerators(Generators, NewPosition, 300+Speed3*10);
438 CreateGenerators(Generators, NewPosition, 200+Speed2*10);
440 CreateGenerators(Generators, NewPosition, 300+Speed4*10);
443 NewLeftRight.
Value += 15;
444 CreateGenerators(Generators, NewLeftRight, 200+Speed1*10);
445 NewLeftRight.
Value -= 15;
446 CreateGenerators(Generators, NewLeftRight, 300+Speed3*10);
447 NewLeftRight.
Value += 15;
448 CreateGenerators(Generators, NewLeftRight, 200+Speed1*10);
449 NewLeftRight.
Value -= 15;
450 CreateGenerators(Generators, NewLeftRight, 300+Speed4*10);
451 return ApplyGenerators(Generators,
false);
455 MA::DeviceGeneratorsMap MAHeadController::FinishRubbing()
462 CreateGenerators(Generators, NewPosition, 700);
465 CreateGenerators(Generators, NewLeftRight, 700);
468 Generators.AddDelayToNeckJoints();
472 return ApplyGenerators(Generators,
false);
476 MA::DeviceGeneratorsMap MAHeadController::DoCircle()
484 int CircleRadiusX = RadiusXDirection == 1 ? RadiusX : -RadiusX;
495 NewLeftRight.
Value -= CircleRadiusX;
497 NewLeftRight.
Value += CircleRadiusX;
499 NewLeftRight.
Value += CircleRadiusX;
501 NewLeftRight.
Value -= CircleRadiusX;
503 return ApplyGenerators(Generators,
true);
507 MA::DeviceGeneratorsMap MAHeadController::HappyMoveToUp()
515 int CircleRadiusX = RadiusXDirection == 1 ? RadiusX : -RadiusX;
522 NewLeftRight.
Value -= CircleRadiusX;
524 NewLeftRight.
Value += CircleRadiusX;
526 return ApplyGenerators(Generators,
true);
530 std::string MAHeadController::DoHappyMovement()
532 std::string TransitionName;
537 TransitionName = DoCircleStr;
541 TransitionName = HappyMoveToUpStr;
544 return TransitionName;
548 MA::DeviceGeneratorsMap MAHeadController::LookAroundLying()
558 for (
int i = 0; i < Motions; ++i)
562 Generators.AddDelayToNeckJoints(DelayDurations[i]);
563 CreateGenerators(Generators,
MALeftRightTurned(LeftRightValues[i]), Durations[i],
569 return ApplyGenerators(Generators,
true);
573 MA::DeviceGeneratorsMap MAHeadController::LookAroundSitting()
583 for (
int i = 0; i < Motions; ++i)
587 Generators.AddDelayToNeckJoints(DelayDurations[i]);
588 CreateGenerators(Generators,
MALeftRightTurned(LeftRightValues[i]), Durations[i],
594 return ApplyGenerators(Generators,
true);
598 MA::DeviceGeneratorsMap MAHeadController::TurnLeftLying()
604 LeftRight.
Value = -95.0;
607 CreateGenerators(Generators, LeftRight, 1800);
608 CreateGenerators(Generators, Position, 1800);
609 return ApplyGenerators(Generators,
true);
613 MA::DeviceGeneratorsMap MAHeadController::TurnRightLying()
619 LeftRight.
Value = 95.0;
622 CreateGenerators(Generators, LeftRight, 1800);
623 CreateGenerators(Generators, Position, 1800);
624 return ApplyGenerators(Generators,
true);
628 MA::DeviceGeneratorsMap MAHeadController::TurnLeftSitting()
634 LeftRight.
Value = -95.0;
637 CreateGenerators(Generators, LeftRight, 1800);
638 CreateGenerators(Generators, Position, 1800);
639 return ApplyGenerators(Generators,
true);
643 MA::DeviceGeneratorsMap MAHeadController::TurnRightSitting()
649 LeftRight.
Value = 95.0;
652 CreateGenerators(Generators, LeftRight, 1800);
653 CreateGenerators(Generators, Position, 1800);
654 return ApplyGenerators(Generators,
true);
659 void MAHeadController::Stop()
667 void MAHeadController::PrintFeatures()
669 MC_LOG(
"Head: LeftRight: %1.2f ForeBack: %1.2f UpDown: %1.2f",
675 bool MAHeadController::IsPowerOn(MA::HeadJointsType joint)
const 679 if (joint == MA::NeckPan)
681 if (joint == MA::NeckTilt1)
683 if (joint == MA::NeckTilt2)
686 if (unlikely(!Device))
688 MC_ERROR(
"The motor device has not been created!");
691 return Device->IsPowerOn();
695 void MAHeadController::SetPower(MA::HeadJointsType joint,
bool new_state)
697 if (joint == MA::EarLeft || joint == MA::EarRight)
699 MC_WARNING(
"The ear motors do not have power on/off states.");
704 if (joint == MA::NeckPan)
706 if (joint == MA::NeckTilt1)
708 if (joint == MA::NeckTilt2)
711 if (unlikely(!Device))
713 MC_ERROR(
"The motor device has not been created!");
717 if (Device->IsPowerOn() != new_state)
719 Device->SetPower(new_state);
724 void MAHeadController::RegisterDeviceUpdates()
726 MA_SIGNAL_CONNECT(
NeckPan->DeviceValueChangeSignal, MAHeadController::NotifyDeviceUpdate);
727 MA_SIGNAL_CONNECT(
NeckTilt1->DeviceValueChangeSignal, MAHeadController::NotifyDeviceUpdate);
728 MA_SIGNAL_CONNECT(
NeckTilt2->DeviceValueChangeSignal, MAHeadController::NotifyDeviceUpdate);
732 void MAHeadController::NotifyDeviceUpdate(
MADevice& device)
734 if (!device.HasSensorValue())
745 HeadPosition.FromJointDegrees((
int)
NeckTilt1->GetSensorValue(), (int)
NeckTilt2->GetSensorValue());
750 void MAHeadController::Stop(MA::HeadJointsType joint)
754 if (joint == MA::NeckPan)
756 if (joint == MA::NeckTilt1)
758 if (joint == MA::NeckTilt2)
763 MC_ERROR(
"The device has not been created!");
766 Device->ClearGenerators();
775 if (dynamic_cast<const MALeftRightTurned*>(&feature))
779 generators.AddGeneratorToNeckPan(*Generator);
787 generators.AddGeneratorToNeckTilt1(*Generator);
790 generators.AddGeneratorToNeckTilt2(*Generator);
800 return MA::DeviceGeneratorsMap();
802 MA::DeviceGeneratorsMap DeviceGenerators;
809 NeckTilt1->AddGeneratorContainer(*Container);
810 MA_INSERT_DEVICEGENERATOR(DeviceGenerators,
NeckTilt1.get(), Container);
814 NeckTilt2->AddGeneratorContainer(*Container);
815 MA_INSERT_DEVICEGENERATOR(DeviceGenerators,
NeckTilt2.get(), Container);
819 NeckPan->AddGeneratorContainer(*Container);
820 MA_INSERT_DEVICEGENERATOR(DeviceGenerators,
NeckPan.get(), Container);
821 return DeviceGenerators;
827 MA::DeviceGeneratorsMap DeviceGenerators;
829 generators.Normalize();
830 if (generators.NeckPan)
832 SetPower(MA::NeckPan,
true);
838 NeckPan->AddGeneratorContainer(*generators.NeckPan);
839 DeviceGenerators.insert(MA::DeviceGeneratorPair(
NeckPan.get(), generators.NeckPan));
842 if (generators.NeckTilt1)
844 SetPower(MA::NeckTilt1,
true);
850 NeckTilt1->AddGeneratorContainer(*generators.NeckTilt1);
851 DeviceGenerators.insert(MA::DeviceGeneratorPair(
NeckTilt1.get(), generators.NeckTilt1));
854 if (generators.NeckTilt2)
856 SetPower(MA::NeckTilt2,
true);
862 NeckTilt2->AddGeneratorContainer(*generators.NeckTilt2);
863 DeviceGenerators.insert(MA::DeviceGeneratorPair(
NeckTilt2.get(), generators.NeckTilt2));
865 return DeviceGenerators;
MANum< float > Value
Current value.
virtual void UpdateRobotState(MARobotState &state) override
Update the robot state.
#define MA_RANDOM_POINT_1(_variable_name, _min, _max)
Set a random point with one value.
void SetTransferFunction(FunctionType function)
Set the transfer function.
bool MCRangeCheck(const T &value, const T &min, const T &max)
Range check of a value.
boost::shared_ptr< MADevice > NeckTilt1
Neck tilt1.
MANum< float > UpDownValue
Current up-down direction value.
Helper class to build head transition generators.
virtual MA::DeviceGeneratorsMap GetSkitTransitionGenerators(MA::SkitBaseSPtr skit) override
Get skit transition generators.
boost::shared_ptr< MAMicrophoneDevice > Microphone
Microphone.
virtual void FromJointDegree(int degree) override
Compute from a joint degree value.
boost::shared_ptr< MADevice > NeckTilt2
Neck tilt2.
#define MC_ERROR(...)
Error macro.
#define MC_WARNING(...)
Warning macro.
Base class for the controllers.
T MCAbs(const T &value)
Calculate absolute value.
float RubbingCenterForeBack
Store the center (fore-back) position of the rubbing.
A base class for the limb features.
static FunctionType GetRandomFunction()
Get a random generator function type.
MAHeadController()
Class constructor.
The left-right turning feature of the head.
boost::shared_ptr< MADevice > NeckPan
Neck pan.
Generator container class.
virtual MA::DeviceList GetMotors() const override
Get the list of the motors.
boost::shared_ptr< MACameraDevice > Camera
Camera.
MAGeneratorContainer * GetGeneratorContainer(MA::Leg::LegTypes leg, MA::LegJointsType joint) const
Get generator container for a leg joint.
virtual ~MAHeadController()
Class destructor.
Motion sequence (motion skit) class.
boost::shared_ptr< MADevice > CrownButton
Crown sensor.
virtual int StartTransition(const std::string &name)
Start a transition.
Base class for generators.
boost::scoped_ptr< MAAudio > Audio
Audio input/output.
float MCFloatInfinity()
Get float infinity.
float RubbingCenterLeftRight
Store the center (left-right) position of the rubbing.
A wrapper class to cover boost::thread_specific_ptr/folly::ThreadLocal API on certain targets...
#define MC_LOG(...)
Debug macro.
#define MA_RANDOM_POINT_N(_variable_name, _count, _min, _max)
Set a random point with a value list.
MANum< float > ForeBackValue
Current fore-back direction value.
boost::shared_ptr< MADevice > ChinButton
Chin sensor.
The fore-back, up-down position of the head.
virtual int ToJointDegree() const =0
Transform to a joint degree value.
boost::scoped_ptr< MAHead > Head
Head.