forked from FRC1296/RHSRobot2015
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutonomousBase.cpp
152 lines (133 loc) · 3.11 KB
/
AutonomousBase.cpp
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* The AutonomousBase component class handles basic autonomous functionallity.
*/
//Local
#include "AutonomousBase.h"
//Robot
#include "ComponentBase.h"
#include "RobotParams.h"
//Built-In
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
extern "C" {
static void *RunAuto(void *pBase)
{
((AutonomousBase *)pBase)->AutoTask();
return(0);
};
}
AutonomousBase::AutonomousBase()
: ComponentBase(AUTONOMOUS_TASKNAME, AUTONOMOUS_QUEUE, AUTONOMOUS_PRIORITY)
{
lineNumber = 0;
bInAutoMode = false;
iExecTaskID = -1;
}
AutonomousBase::~AutonomousBase() //Destructor
{
}
void AutonomousBase::Init() //Initializes the autonomous component
{
LoadScriptFile();
}
void AutonomousBase::OnStateChange() //Handles state changes
{
pthread_attr_t attr;
struct sched_param schedparam;
if(localMessage.command == COMMAND_ROBOT_STATE_AUTONOMOUS)
{
// set thread attributes to default values
pthread_attr_init(&attr);
// we do not wait for threads to exit
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
// each thread has a unique scheduling algorithm
pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
// we'll force the priority of threads or tasks
pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
// we'll use static real time priority levels
schedparam.__sched_priority = AUTOEXEC_PRIORITY;
pthread_attr_setschedparam(&attr, &schedparam);
pthread_create(&iExecTaskID, &attr, RunAuto, this);
pthread_setname_np(iExecTaskID, AUTOEXEC_TASKNAME);
}
else if((localMessage.command == COMMAND_ROBOT_STATE_TELEOPERATED) ||
(localMessage.command == COMMAND_ROBOT_STATE_DISABLED))
{
if(iExecTaskID > 0)
{
pthread_cancel(iExecTaskID);
iExecTaskID = -1;
}
}
}
void AutonomousBase::Run() //Autonomous logic
{
switch(localMessage.command)
{
case COMMAND_AUTONOMOUS_RUN:
if(lineNumber < AUTONOMOUS_SCRIPT_LINES)
{
if(script[lineNumber].empty() == false)
{
Evaluate(script[lineNumber]);
++lineNumber;
}
}
break;
case COMMAND_CHECKLIST_RUN:
if(lineNumber < AUTONOMOUS_CHECKLIST_LINES)
{
Evaluate(script[lineNumber]);
++lineNumber;
}
break;
default:
break;
}
}
void AutonomousBase::LoadScriptFile()
{
ifstream scriptStream(AUTONOMOUS_SCRIPT_FILEPATH);
if(scriptStream.is_open())
{
// don't read the script if executing it!
if(bInAutoMode == false)
{
for(int i = 0; i < AUTONOMOUS_SCRIPT_LINES; ++i)
{
if(!scriptStream.eof())
{
getline(scriptStream, script[i]);
cout << script[i] << endl;
}
else
{
script[i].clear();
}
}
}
else
{
printf("Attempt to read script in auto mode\n");
}
scriptStream.close();
}
else
{
printf("No auto file found\n");
}
}
void AutonomousBase::AutoTask()
{
lineNumber = 0;
while(lineNumber < AUTONOMOUS_SCRIPT_LINES)
{
if(script[lineNumber].empty() == false)
{
Evaluate(script[lineNumber]);
++lineNumber;
}
}
}