-
Notifications
You must be signed in to change notification settings - Fork 0
/
IRobotWrapper.h
104 lines (96 loc) · 2.94 KB
/
IRobotWrapper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#ifndef _IROBOTWRAPPER_H
#define _IROBOTWRAPPER_H
#include "IRobotBaseWrapper.h"
#include "IRobotKITWrapper.h"
#include "IRobotScoutWrapper.h"
#include "IRobotAWheelWrapper.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/**
* Very clever the construction with the singleton pattern with RobotBase that returns the right
* robot instance. There is no need for that here. It makes more sense to separate the basis functionality
* from the constructor/destructor functionality.
* SO: http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons
*/
class IRobotFactory {
public:
//! Default robot is a KIT robot
IRobotFactory(): robot(NULL), firmware(new RobotBase::BoardFirmware()) {
}
~IRobotFactory() {
if (robot != NULL) delete robot;
robot = NULL;
delete firmware;
}
RobotBase * GetRobot() {
if (robot != NULL) return robot;
GetType();
return robot;
}
RobotBase::RobotType GetType() {
char *robot_type = getenv("ROBOT");
if (robot_type != NULL) {
if (!strcmp(robot_type, "activewheel"))
firmware->type = RobotBase::ACTIVEWHEEL;
else if (!strcmp(robot_type, "scout"))
firmware->type = RobotBase::SCOUTBOT;
else if (!strcmp(robot_type, "backbone"))
firmware->type = RobotBase::KABOT;
else {
fprintf(stderr, "ERROR! The environmental variable \"ROBOT\" is not properly defined!\n");
fprintf(stderr, "run export ROBOT=activewheel, export ROBOT=scout, or export ROBOT=backbone\n");
}
} else {
fprintf(stderr, "ERROR! There is no environmental variable \"ROBOT\" defined!\n");
fprintf(stderr, "run export ROBOT=activewheel, export ROBOT=scout, or export ROBOT=backbone\n");
}
if (robot == NULL) {
SetRobot(firmware->type);
} else {
printf("Robot is already set before\n");
}
return firmware->type;
}
void SetRobot(RobotBase::RobotType type) {
int ret = 0;
switch(type) {
case RobotBase::KABOT:
printf("%s to KABOT\n",__func__);
robot = new KaBot();
if ((ret = ((KaBot*)robot)->init(RobotFunc::USES_SPI)) != 0) {
printf("Error (%d) on init(). Exiting\n", ret);
} else {
printf("Initialized SPI daemon\n");
}
break;
case RobotBase::SCOUTBOT:
printf("%s to SCOUTBOT\n",__func__);
robot = new ScoutBot();
if ((ret = ((ScoutBot*)robot)->init(RobotFunc::USES_SPI)) != 0) {
printf("Error (%d) on init(). Exiting\n", ret);
} else {
printf("Initialized SPI daemon\n");
}
break;
case RobotBase::ACTIVEWHEEL:
printf("%s to ACTIVEWHEEL\n",__func__);
robot = new ActiveWheel();
if ((ret = ((ActiveWheel*)robot)->init(RobotFunc::USES_SPI)) != 0) {
printf("Error (%d) on init(). Exiting\n", ret);
} else {
printf("Initialized SPI daemon\n");
}
break;
case RobotBase::UNKNOWN: default:
fprintf(stderr, "Unknown robot, a KIT robot will be created\n");
robot = new KaBot();
break;
}
RobotBase::Instance(robot);
}
private:
RobotBase *robot;
RobotBase::BoardFirmware *firmware;
};
#endif // _IROBOTWRAPPER_H