Arduino  1.8.10
Sensors.cpp
[詳解]
1 /*
2  Copyright (c) 2012 Arduino LLC. All right reserved.
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #include "ArduinoRobot.h"
20 #include "Multiplexer.h"
21 #include "Wire.h"
22 bool RobotControl::digitalRead(uint8_t port){
23  uint8_t type=_getTypeCode(port);
24  switch(type){
25  case TYPE_TOP_TK:
26  return _digitalReadTopMux(port);
27  break;
28  case TYPE_TOP_TKD:
29  return _digitalReadTopPin(port);
30  break;
31  case TYPE_BOTTOM_TK:
32  return _requestDigitalRead(port);
33  break;
34  }
35 }
36 int RobotControl::analogRead(uint8_t port){
37  uint8_t type=_getTypeCode(port);
38  switch(type){
39  case TYPE_TOP_TK:
40  return _analogReadTopMux(port);
41  break;
42  case TYPE_TOP_TKD:
43  return _analogReadTopPin(port);
44  break;
45  case TYPE_BOTTOM_TK:
46  return _requestAnalogRead(port);
47  break;
48  }
49 }
50 void RobotControl::digitalWrite(uint8_t port, bool value){
51  uint8_t type=_getTypeCode(port);
52  switch(type){
53  case TYPE_TOP_TK:
54  //Top TKs can't use digitalWrite?
55  break;
56  case TYPE_TOP_TKD:
57  _digitalWriteTopPin(port, value);
58  break;
59  case TYPE_BOTTOM_TK:
60  _requestDigitalWrite(port, value);
61  break;
62  }
63 }
64 void RobotControl::analogWrite(uint8_t port, uint8_t value){
65  if(port==TKD4)
66  ::analogWrite(port,value);
67 }
68 
69 uint8_t RobotControl::_getTypeCode(uint8_t port){
70  switch(port){
71  case TK0:
72  case TK1:
73  case TK2:
74  case TK3:
75  case TK4:
76  case TK5:
77  case TK6:
78  case TK7:
79  return TYPE_TOP_TK;
80  break;
81 
82  case TKD0:
83  case TKD1:
84  case TKD2:
85  case TKD3:
86  case TKD4:
87  case TKD5:
88  case LED1:
89  return TYPE_TOP_TKD;
90  break;
91 
92  case B_TK1:
93  case B_TK2:
94  case B_TK3:
95  case B_TK4:
96  return TYPE_BOTTOM_TK;
97  break;
98  }
99 }
100 uint8_t RobotControl::_portToTopMux(uint8_t port){
101  switch(port){
102  case TK0:
103  return 0;
104  case TK1:
105  return 1;
106  case TK2:
107  return 2;
108  case TK3:
109  return 3;
110  case TK4:
111  return 4;
112  case TK5:
113  return 5;
114  case TK6:
115  return 6;
116  case TK7:
117  return 7;
118  }
119 }
120 uint8_t RobotControl::_topDPortToAPort(uint8_t port){
121  switch(port){
122  case TKD0:
123  return A1;
124  case TKD1:
125  return A2;
126  case TKD2:
127  return A3;
128  case TKD3:
129  return A4;
130  case TKD4:
131  return A7;
132  case TKD5:
133  return A11;
134  }
135 }
136 int* RobotControl::parseMBDPort(uint8_t port){
137  //Serial.println(port);
138  switch(port){
139  case B_TK1:
140  return &motorBoardData._B_TK1;
141  case B_TK2:
142  return &motorBoardData._B_TK2;
143  case B_TK3:
144  return &motorBoardData._B_TK3;
145  case B_TK4:
146  return &motorBoardData._B_TK4;
147 
148  /*
149  case B_IR0:
150  return &motorBoardData._B_IR0;
151  case B_IR1:
152  return &motorBoardData._B_IR1;
153  case B_IR2:
154  return &motorBoardData._B_IR2;
155  case B_IR3:
156  return &motorBoardData._B_IR3;
157  case B_IR4:
158  return &motorBoardData._B_IR4;*/
159  }
160 }
161 int RobotControl::get_motorBoardData(uint8_t port){
162  return *parseMBDPort(port);
163 }
164 void RobotControl::set_motorBoardData(uint8_t port, int data){
165  *parseMBDPort(port)=data;
166 }
167 
168 bool RobotControl::_digitalReadTopMux(uint8_t port){
169  uint8_t num=_portToTopMux(port);
170  return Multiplexer::getDigitalValueAt(num);
171 }
172 
173 int RobotControl::_analogReadTopMux(uint8_t port){
174  uint8_t num=_portToTopMux(port);
175  return Multiplexer::getAnalogValueAt(num);
176 }
177 
178 bool RobotControl::_digitalReadTopPin(uint8_t port){
179  return ::digitalRead(port);
180 }
181 int RobotControl::_analogReadTopPin(uint8_t port){
182  uint8_t aPin=_topDPortToAPort(port);
183  return ::analogRead(aPin);
184 }
185 void RobotControl::_digitalWriteTopPin(uint8_t port, bool value){
186  ::digitalWrite(port, value);
187 }
188 
189 bool RobotControl::_requestDigitalRead(uint8_t port){
190  messageOut.writeByte(COMMAND_DIGITAL_READ);
191  messageOut.writeByte(port);//B_TK1 - B_TK4
192  messageOut.sendData();
193  delay(10);
194  if(messageIn.receiveData()){
195  //Serial.println("*************");
196  uint8_t cmd=messageIn.readByte();
197  //Serial.print("cmd: ");
198  //Serial.println(cmd);
199  if(!(cmd==COMMAND_DIGITAL_READ_RE))
200  return false;
201 
202  uint8_t pt=messageIn.readByte(); //Bottom TK port codename
203  //Serial.print("pt: ");
204  //Serial.println(pt);
205  set_motorBoardData(pt,messageIn.readByte());
206  return get_motorBoardData(port);
207  }
208 }
209 int RobotControl::_requestAnalogRead(uint8_t port){
210  messageOut.writeByte(COMMAND_ANALOG_READ);
211  messageOut.writeByte(port);//B_TK1 - B_TK4
212  messageOut.sendData();
213  delay(10);
214  if(messageIn.receiveData()){
215  uint8_t cmd=messageIn.readByte();
216  //Serial.println("*************");
217  //Serial.print("cmd: ");
218  //Serial.println(cmd);
219  if(!(cmd==COMMAND_ANALOG_READ_RE))
220  return false;
221 
222  uint8_t pt=messageIn.readByte();
223  //Serial.print("pt: ");
224  //Serial.println(pt);
225  set_motorBoardData(pt,messageIn.readInt());
226  return get_motorBoardData(port);
227  }
228 }
229 void RobotControl::_requestDigitalWrite(uint8_t selector, uint8_t value){
230  messageOut.writeByte(COMMAND_DIGITAL_WRITE);
231  messageOut.writeByte(selector);//B_TK1 - B_TK4
232  messageOut.writeByte(value);
233  messageOut.sendData();
234 }
235 
236 
237 
238 
239 
241  messageOut.writeByte(COMMAND_READ_IR);
242  messageOut.sendData();
243  delay(10);
244  if(messageIn.receiveData()){
245  if(messageIn.readByte()==COMMAND_READ_IR_RE){
246  for(int i=0;i<5;i++){
247  IRarray[i]=messageIn.readInt();
248  }
249  }
250  }
251 }
252 
254  return ::analogRead(POT);
255 }
256 
258  messageOut.writeByte(COMMAND_READ_TRIM);
259  messageOut.sendData();
260  delay(10);
261  if(messageIn.receiveData()){
262  uint8_t cmd=messageIn.readByte();
263  if(!(cmd==COMMAND_READ_TRIM_RE))
264  return false;
265 
266  uint16_t pt=messageIn.readInt();
267  return pt;
268  }
269 }
270 
272  return Compass::getReading();
273 }
274 
275 /*
276 void RobotControl::beginUR(uint8_t pinTrigger, uint8_t pinEcho){
277  pinTrigger_UR=pinTrigger;
278  pinEcho_UR=pinEcho;
279 
280  pinMode(pinEcho_UR, INPUT);
281  pinMode(pinTrigger_UR, OUTPUT);
282 }
283 uint16_t RobotControl::getDistance(){
284  digitalWrite(pinTrigger_UR, LOW); // Set the trigger pin to low for 2uS
285  delayMicroseconds(2);
286  digitalWrite(pinTrigger_UR, HIGH); // Send a 10uS high to trigger ranging
287  delayMicroseconds(10);
288  digitalWrite(pinTrigger_UR, LOW); // Send pin low again
289  uint16_t distance = pulseIn(pinEcho_UR, HIGH); // Read in times pulse
290  distance= distance/58; // Calculate distance from time of pulse
291  return distance;
292 }*/
#define TYPE_TOP_TKD
Definition: ArduinoRobot.h:93
boolean receiveData()
#define LED1
Definition: led.h:61
#define COMMAND_READ_TRIM_RE
Definition: ArduinoRobot.h:76
#define B_TK3
Definition: ArduinoRobot.h:118
void updateIR()
Definition: Sensors.cpp:240
uint8_t readByte()
#define TK0
Definition: ArduinoRobot.h:97
#define COMMAND_ANALOG_READ_RE
Definition: ArduinoRobot.h:69
uint8_t i
bool getDigitalValueAt(uint8_t num)
Definition: Multiplexer.cpp:52
float getReading()
Definition: Compass.cpp:25
void delay(unsigned long)
Definition: wiring.c:106
void analogWrite(uint8_t port, uint8_t value)
Definition: Sensors.cpp:64
#define COMMAND_READ_IR
Definition: ArduinoRobot.h:72
#define COMMAND_ANALOG_READ
Definition: ArduinoRobot.h:68
#define COMMAND_DIGITAL_READ
Definition: ArduinoRobot.h:70
int analogRead(uint8_t port)
Definition: Sensors.cpp:36
int knobRead()
Definition: Sensors.cpp:253
void writeByte(uint8_t dat)
#define TK6
Definition: ArduinoRobot.h:103
#define B_TK2
Definition: ArduinoRobot.h:117
#define COMMAND_DIGITAL_READ_RE
Definition: ArduinoRobot.h:71
#define COMMAND_READ_IR_RE
Definition: ArduinoRobot.h:73
#define COMMAND_DIGITAL_WRITE
Definition: ArduinoRobot.h:67
bool digitalRead(uint8_t port)
Definition: Sensors.cpp:22
#define TK2
Definition: ArduinoRobot.h:99
int analogRead(uint8_t)
Definition: wiring_analog.c:38
int getAnalogValueAt(uint8_t num)
Definition: Multiplexer.cpp:47
#define TK4
Definition: ArduinoRobot.h:101
int trimRead()
Definition: Sensors.cpp:257
#define TK3
Definition: ArduinoRobot.h:100
#define TK5
Definition: ArduinoRobot.h:102
int digitalRead(uint8_t)
#define B_TK1
Definition: ArduinoRobot.h:116
uint16_t IRarray[5]
Definition: ArduinoRobot.h:223
#define TYPE_TOP_TK
Definition: ArduinoRobot.h:92
void digitalWrite(uint8_t port, bool value)
Definition: Sensors.cpp:50
uint16_t compassRead()
Definition: Sensors.cpp:271
#define COMMAND_READ_TRIM
Definition: ArduinoRobot.h:75
uint8_t type
Definition: FatStructs.h:62
#define TK1
Definition: ArduinoRobot.h:98
#define TK7
Definition: ArduinoRobot.h:104
#define TYPE_BOTTOM_TK
Definition: ArduinoRobot.h:94
#define B_TK4
Definition: ArduinoRobot.h:119