Exercise: Detecing Pink Balls and Text Messages
In order to access the additional members that VisionObjectEvent and
TextMsgEvent provide to extend EventBase, we must cast the event
argument to the correct type. This is done using the
dynamic_cast operator, as shown below.
#ifndef INCLUDED_DstBehavior_h_
#define INCLUDED_DstBehavior_h_
#include "Behaviors/BehaviorBase.h"
#include "Events/EventRouter.h"
#include "Events/TextMsgEvent.h"
#include "Events/VisionObjectEvent.h"
#include "Shared/ProjectInterface.h"
class DstBehavior : public BehaviorBase {
public:
DstBehavior() : BehaviorBase("DstBehavior") {}
virtual void DoStart() {
BehaviorBase::DoStart();
std::cout << getName() << " is starting up." << std::endl;
erouter->addListener(this,EventBase::buttonEGID); // subscribe to all button events
erouter->addListener(this,EventBase::visObjEGID, // and pink ball detection events
ProjectInterface::visPinkBallSID);
erouter->addListener(this,EventBase::textmsgEGID); // and text message events
}
virtual void DoStop() {
std::cout << getName() << " is shutting down." << std::endl;
BehaviorBase::DoStop();
}
virtual void processEvent(const EventBase &event) {
switch ( event.getGeneratorID() ) {
case EventBase::visObjEGID: {
const VisionObjectEvent &visev = dynamic_cast<const VisionObjectEvent&>(event);
if ( visev.getTypeID() == EventBase::activateETID ||
visev.getTypeID() == EventBase::statusETID )
std::cout << "Saw a pink ball at ( " <<
visev.getCenterX() << " , " << visev.getCenterY() << " )" << std::endl;
else // deactivateETID
std::cout << "Lost sight of the pink ball." << std::endl;
break; };
case EventBase::textmsgEGID: {
const TextMsgEvent &txtev = dynamic_cast<const TextMsgEvent&>(event);
std::cout << "Heard: '" << txtev.getText() << "'" << std::endl;
break; };
case EventBase::buttonEGID:
std::cout << getName() << " got event: " << event.getDescription() << std::endl;
break;
default:
std::cout << "Unexpected event: " << event.getDescription() << std::endl;
}
}
};
#endif
|
Recompile and update the memory stick by doing "make update". Then
boot up the AIBO, open a telnet connection to port 59000, and activate
DstBehavior. Using the ControllerGUI, call up the SegCam window so
you can see what the AIBO sees. Make sure the camera isn't pointed at
anything red or pink. Now wave the pink ball in front of the AIBO and
see what happens.
In the ControllerGUI window, type !msg hello in the text
box and see what happens.
|