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.
#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() {
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;
}
virtual void doEvent() {
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;
}
}
};
REGISTER_BEHAVIOR(DstBehavior);
|
Recompile, run Tekkotsu again, and activate DstBehavior. Using the
ControllerGUI, call up the SegCam window so you can see what the robot
sees. Make sure the camera isn't pointed at anything red or pink.
Now wave the pink ball in front of the robot and see what happens.
In the ControllerGUI window, type !msg hello in the text
box and see what happens.
|