-
Notifications
You must be signed in to change notification settings - Fork 2
/
eqep.cpp
executable file
·156 lines (116 loc) · 3.01 KB
/
eqep.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
153
154
155
156
/* Created by: Zongyao Jin
*
*/
#include "eqep.h"
#include "util.h"
#include <iostream>
#include <fstream>
#include <cstdlib>
namespace ZJ {
EQEP::EQEP(bool i) {
if (i == 0) this->path = EQEP_A;
else this->path = EQEP_B;
this->setZero();
this->position = this->getPosition();
// this->streamOpen();
}
int EQEP::setZero(){
return write(this->path, EQEP_POSITION, 0);
}
int EQEP::getPosition(){
return atoi(read(this->path, EQEP_POSITION).c_str());
}
void EQEP::updateSensor() {
position = this->getPosition();
}
float EQEP::getRad(){
this->updateSensor();
return (position/COUNTS_PER_REV * 2.0f * PI);
}
float EQEP::getDeg(){
this->updateSensor();
return (position/COUNTS_PER_REV * 360.0f);
}
// ----------------------
// int EQEP::streamOpen() {
// is.open((path + "position").c_str(), std::ifstream::binary);
// std::cout << "in streamOpen() " << std::endl;
// return 0;
// }
// int EQEP::streamPosition() {
// std::cout << "in streamPosition() 1" << std::endl;
// // get length of file:
// is.seekg (0, is.end);
// int length = is.tellg();
// is.seekg (0, is.beg);
// std::cout << "in streamPosition() 2" << std::endl;
// char * buffer = new char [length];
// is.read(buffer,length);
// std::cout << int(&buffer) << std::endl;
// if (is) {
// int(&buffer) >> this->position;
// return 0;
// } else {
// return -1;
// }
// std::cout << "in streamPosition() 4" << std::endl;
// std::cout << buffer << std::endl;
// delete[] buffer;
// }
// float EQEP::streamRad() {
// this->streamPosition();
// return (position/COUNTS_PER_REV * 2.0f * PI);
// }
// float EQEP::streamDeg() {
// std::cout << "in streamDeg() " << std::endl;
// this->streamPosition();
// return (position/COUNTS_PER_REV * 360.0f);
// }
// int EQEP::streamClose(){
// is.close();
// return 0;
// }
// ----------------------
// int EQEP::streamOpen() {
// // Read the position
// fp = fopen((this->path + "/position").c_str(), "r");
// if(fp == NULL)
// {
// // Error, break out
// std::cerr << "[eQEP " << this->path << "] Unable to open position for read" << std::endl;
// return -1;
// }
// return 0;
// }
// int EQEP::streamPosition() {
// // Write the desired value to the file
// fscanf(fp, "%d", &position);
// return this->position;
// }
// float EQEP::streamRad() {
// this->streamPosition();
// return (position/COUNTS_PER_REV * 2.0f * PI);
// }
// float EQEP::streamDeg() {
// this->streamPosition();
// return (position/COUNTS_PER_REV * 360.0f);
// }
// int EQEP::streamClose(){
// fclose(fp);
// return 0;
// }
// ----------------------
int EQEP::run(){
// It seems that if the DTO is deployed, enable is always 1.
// And we don't have permission to enable it again.
return write(this->path, EQEP_ENABLE, 1);
}
bool EQEP::isRunning(){
string running = read(this->path, EQEP_ENABLE);
return (running=="1");
}
int EQEP::stop(){
return write(this->path, EQEP_ENABLE, 0);
}
EQEP::~EQEP() {}
} /* namespace ZJ */