-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.h
185 lines (146 loc) · 4.04 KB
/
sensor.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*!
* @file sensor.h v1.0
* @Copyright © 2018 Nobuhiro Funato and Shogo Arato
* @date 2018.11.16
*
* Released under the MIT license.
* see https://opensource.org/licenses/MIT
*/
#ifndef SENSOR_H
#define SENSOR_H
#include "deftype.h"
#include "mbed.h"
/**
* @brief
* PSDセンサによって、物体までの距離を検知する <br>
* アナログ通信の通信方式で距離検知
*/
class DistanceSensor{
private:
AnalogIn _analog_value;
public:
DistanceSensor( PinName );
int get_val();
operator int (){
return get_val();
}
};
/**
* @fn I^2Cの通信方式で距離検知
*/
class gp2y_i2c{
private:
I2C i2c;
char cmd[2];
char Distance_adrs[2];
char i2c_read(int,char);
public:
gp2y_i2c(PinName, PinName);
gp2y_i2c(PinName, PinName, int);
void set_frequency(int);
int distance(char);
};
class RotaryEncoder{
private:
unsigned int data;
unsigned int angle;
unsigned int status;
long int integra_val;
int error;
int speed_cnt;
int speed_val;
int speed_temp_val;
bool _cwccw;
unsigned int temp_angle;
SPI *encoder;
int ans;
int temp_cal;
int over_temp_cal;
int diff;
int diff_error[300];
public:
RotaryEncoder(PinName mosi,PinName miso,PinName clock,bool cwccw)
{
encoder = new SPI(mosi,miso,clock);
encoder->format( 16,2 );
encoder->frequency(500000);
speed_val=speed_cnt=temp_angle=0;
error=0x00;
_cwccw = cwccw;
}
~RotaryEncoder()
{
delete encoder;
}
void Reset(){
integra_val = 0;
}
int Error( int return_mode=0, int i=0 ){
switch(return_mode){
case 1:
return diff_error[i];
default:
return error;
}
}
int Status(){
return status;
}
int Angle(){
int val = angle;
val = (double)val*0.351;
return val;
}
int Speed(bool mode=false){
if(mode){
if( speed_cnt >= 50 ){
speed_val = speed_temp_val;
speed_temp_val=speed_cnt=0;
}else speed_temp_val+= diff;
speed_cnt++;
}else{
if( _cwccw )return speed_val;
else return -speed_val;
}
return false;
}
long int Integration_Value(){
long int val = integra_val;
val = integra_val;
if( _cwccw )return val;
else return -val;
}
void encoder_calc(){
int temp_cal;
data = encoder->write(0x00);
angle = (data >> 5) & 0x3ff;
status = data & 0x1f;
temp_cal = (int)(angle - temp_angle);
if( temp_cal == 0 ){
error=0x00;
diff = 0;
}else{
diff = integra_val;
if( temp_cal == 1 || temp_cal == 1023 ){
integra_val++;
}else if( temp_cal == -1 || temp_cal == -1023 ){
integra_val--;
}else if( temp_cal <= 30 && temp_cal > 1 ){
integra_val += temp_cal;
}else if( temp_cal >= -30 && temp_cal < -1 ){
integra_val += temp_cal;
}else if( temp_cal > 1000 ){
integra_val += angle-1023-temp_angle;
}else if( temp_cal < -1000 ){
integra_val += 1023-temp_angle+angle;
}else{
//if(error<300)diff_error[error];
error++;
}
diff = integra_val-diff;
}
RotaryEncoder::Speed(true);
temp_angle = angle;
}
};
#endif