Ok, after failing (more-or-less miserably) with the previous attempt at IR distance detection, I went out and purchased 4 of the Sharp GP2D12 from an eBay Store and put them to work. Finally I had a detection system working, but before this I had also attempted another method using larger IR emitter/detectors that I'd bought from Dip Micro.
Attempt 1: QRD1114, and other smaller IR detectors
Since my previous attempt had failed, I'd decided that if the smaller emitters could not produce enough light to avoid sunlight/roomlight interference, then boosting both the emitter and detector should help. I'd accidently purchased a collection of 10x emitter/collector pairs of IR diodes and so I put these to work in the typical setup and tested them out. These were both 3mm in diameter and, depending on the resistors used, could emit a lot more light (tested via my digital camera.)
The setup was the same as per usual... mounted horizontally at the end of the tracks to ensure that the light would reflect (as much as possible) off the approaching train.
Working with IR at night-time has it's benefits. Your room is usually lit under artificial light and so the amount of IR in the 'air' is low. I therefore had some pretty good results with this setup, but of course, come daylight, everything went out the window (or in the window, as the case may be.) Since this detector was to be in the back of an engine shed, I'd thought that I could block out the windows and make a little dark room, but my detectors were still too unreliable.
This experiment was, in the end, functional, but not to the degree that I'd wanted and so I therefore opted for the off-the-shelf Sharp detector.
Note: The goal here was to use the pair at the end of the track to sense distance. It now seems that the best method will be to detect 'occupation' and I will again test this method with a series of emitter/detector pairs along the track to sense when a train is approaching.
Attempt 3: Sharp GP2D12
After being concerned about sizing and the minimum distance that these detectors would work from, I decided I'd just bite the bullet and try them out.
I ended up purchasing 4 quite cheaply on eBay and they arrived from the UK in a short amount of time. I then realised that the versions I'd bought were optimised for detection between 10cm and 70cm. This really sucks, as I'd want the range to be much closer to the detector, as 10cm is a long way for a no-detection zone. I then looked at the graphs showing the voltage compared to distance:
Although the 'optimum' detecting distance for unit is around 10cm, the detector still gives valid voltage results right down to around 3cm. The only issue here is that the voltage difference is not always increasing! I therefore have to take the value and use it in different scenarios (i.e. when cruising, braking, stopped, etc...)
The setup was the same as usual:
So, to deal with this, I needed to work on the voltage change... in steps of around 0.25v. If you happen to implement this yourself, you'll notice straight away that the voltage returned by this unit is not constant when the vehicle is stopped or approaching... it's perpetually flitting around +-0.5v and this can be a real nightmare. Due to this I only choose to detect larger changes which means only reading the sensor if it is +-25 of the current read value.
//we should be checking which point we're about to hit first?
153
/*if (a1 > 400 && dir == 0) {
154
spd = 0;
155
dir = !dir;
156
train_status = 0;
157
} else if (a1 > 300 && dir == 0) spd = 70;
158
else if (a1 > 200 && dir == 0) spd = 90;
159
else if (a1 > 150 && dir == 0) spd = 110;
160
else spd = 125;*/
161
if(train_status != 3) {
162
if(sensor > 400) {
163
train_status = 3;
164
dirT = t;
165
} elseif(sensor > 300) {
166
train_status = 2;
167
}
168
} elseif(t - dirT > 500 && train_status == 3) {
169
train_status = 0;
170
dir = !dir;
171
dirT = t;
172
}
173
}
174
175
lcd.clear();
176
lcd.setCursor(0, 0);
177
lcd.print("");
178
lcd.setCursor(0, 0);
179
lcd.print(a1);
180
lcd.setCursor(0, 1);
181
lcd.print("");
182
lcd.setCursor(0, 1);
183
lcd.print(a2);
184
185
lcd.setCursor(5, 0);
186
lcd.print(thresholds[0]);
187
lcd.setCursor(7, 0);
188
lcd.print(thresholds[1]);
189
190
lcd.setCursor(5, 1);
191
lcd.print(thresholds[2]);
192
lcd.setCursor(7, 1);
193
lcd.print(thresholds[3]);
194
195
lcd.setCursor(11, 0);
196
lcd.print(spd);
197
lcd.setCursor(11, 1);
198
lcd.print(dir);
199
lcd.setCursor(15, 1);
200
lcd.print(train_status);
201
202
oldT = t;
203
}
204
205
if(a1 < thresholds[0]) thresholds[0] = a1;
206
if(a1 > thresholds[1]) thresholds[1] = a1;
207
if(a2 < thresholds[2]) thresholds[2] = a2;
208
if(a2 > thresholds[3]) thresholds[3] = a2;
209
210
if(dir == 0) {
211
digitalWrite(3, HIGH);
212
digitalWrite(4, LOW);
213
} else{
214
digitalWrite(4, HIGH);
215
digitalWrite(3, LOW);
216
}
217
218
analogWrite(2, spd);
219
}
From the above, we get the following action... note that the whole process here is automated (throttle, detection, point switching):
Conclusion
This sensor worked much better than the previous attempts... but it's still not the best. I think I might now just grab one of the GP2D120s (as it would simply be plug-and-play) and see what happens. Also lighting plays an affect here too, it might just be easier in the end to have a strip of LDRs to work out where the train is... but everything has it's good and bad side!
The other option will be to have a spaced strip of detectors down one side of the track and emitters on the other. This will be like your tandy/radio-shack store that beep-beeps when you trip the beam... we'll see how well it works.
Wow, those Sharp sensors are way larger than I’d expected. I’m beginning to think that a row of IR beams is going to end being the way to go with this, complex as it might be…
They are huge… they do the job though.
I’m currently fine-tuning a library to manage their output and it’s functioning ok. Time isn’t on my side though.
In the end just tripping IR beams would’ve been the easiest method and you could either accumulate the resistance or multiplex around 4 or 5 beams so as to not waste Arduino pins; but… this is more fun :)
April 2nd, 2010 - 07:35
Wow, those Sharp sensors are way larger than I’d expected. I’m beginning to think that a row of IR beams is going to end being the way to go with this, complex as it might be…
April 6th, 2010 - 11:08
They are huge… they do the job though.
I’m currently fine-tuning a library to manage their output and it’s functioning ok. Time isn’t on my side though.
In the end just tripping IR beams would’ve been the easiest method and you could either accumulate the resistance or multiplex around 4 or 5 beams so as to not waste Arduino pins; but… this is more fun :)