Will this code do what I want it to do?

Status
Not open for further replies.

BlueJay88

New member
Hello,

I'm doing a science experiment (a Kolsky bar experiment, if you're curious) that involves a projectile, and I'm hoping to use a Teensy 3.1 to calculate the projectile speed and adjust the timing of some other experimental equipment accordingly. I need the calculation to happen in real time, because variables like friction in the projectile barrel can change the velocity of the projectile from test to test, so adjusting my experimental timing after each test is over doesn't really accomplish anything.

I have two lasers pointed at two photodiodes, which are outputting TTL signal via BNC cables. The projectile will block laser 1 and then laser 2, causing the photodiode signal to fall, and I'd like to determine the difference in time (~1 millisecond) between each drop in signal. Then, I'd like to use this time and the known distance between the lasers to calculate the projectile velocity, and calculate an additional delay time (which will probably be tens or hundreds of microseconds) based on this velocity. Finally, after waiting for this delay time, I'd like to output another TTL pulse via BNC cable. From start to finish, I'd like this whole process to take no more than 1-2 milliseconds, and shorter is better.

I've cobbled together some code that I think will work, but I'm a total novice, and I don't have all the experimental equipment I need to verify its effectiveness yet. Does this look like it will do what I want, though? If not, can anyone point me in the right direction? Thanks.

Code:

//This assumes that BNC1 is in pin 7 and BNC2 is in pin 8 while BNCout is in pin 9, although I don't know if this makes any sense. Would the ultimate output happen faster if I used different pins?
//Values for laserSeparation, barDistance, and pulsePropagationTime are just arbitrary since I haven't measured them yet
//This assumes that the velocity of the projectile remains constant (i.e., that velocityChangeFactor = 1);

const int laserSeparation = 25;
const int barDistance = 25;
const int pulsePropagationTime = 2;
const int velocityChangeFactor = 1;
int delayTime = 0;
int elapsedTime = 0;
int time1 = 0;
int time2 = 0;

void setup()
{

//Is this necessary?
cli();

//Is this necessary? Would ultimate output happen faster if I changed this to Serial.begin(115200)?
Serial.begin(38400);

pinMode(7, INPUT);
pinMode(8, INPUT);
pinMode(9, OUTPUT);

}

void loop()
{

while (digitalReadFast(7) == HIGH)
{
continue;
}

//when the first laser is blocked
time1 = micros();

while (digitalReadFast(8) == HIGH)
{
continue;
}

//when the second laser is blocked
time2 = micros();

elapsedTime = time2 - time1;
delayTime = velocityChangeFactor*barDistance/(laserSeparation/elapsedTime) + pulsePropagationTime;
delayMicroseconds(delayTime);
digitalWriteFast(9, HIGH);

}
 
That code looks close. Pin 9 looks like it will forever stay high after the first measurement, since you're not writing it LOW at any point.

You definitely don't want cli() at the beginning. The micro() function depends on the SysTick interrupt happening normally.

You didn't mention specifics of the sensors, other that "photodiode". If they really are PIN diodes with proper circuitry, they should be plenty fast enough. But if they're actually NPN photo-sensitive transistors, you might find their response is too slow. CdS photocells are even worse. Teensy obviously can't do anything about the quality of the signals, but I thought this might be worth mentioning. At least a couple other people have tried similar speed measurement projects with cheap CdS photocells, which respond far too slowly for useful speed measurements.
 
Status
Not open for further replies.
Back
Top