Timing task

This forum is for posts that might be considered off-topic but that may be useful or interesting to members. Examples include posts about electronics or programming in general, other microcontrollers or interesting devices, useful websites, etc.
Post Reply
victorf
Posts: 342
Joined: 01 January 2006, 4:08 AM
Location: Schenectady, New York

Timing task

Post by victorf »

I am building an instrument that is designed for a user to track an object optically. Every 15 seconds the instrument makes a measurement of the bearing of the object being tracked and elevation of the optical axis. After making the measurement the ZX-24 makes some calculations and displays the results. In the older instrument, the calculation/display was done during the waiting time for the next observation. This cycle occurs for as long as the observer wishes to collect data. The end of one 15 second tracking period is the beginning of the next. The user must get an audible warning (piezo-buzzer) for the last 5 seconds of the measurement period. I have a buzzer attached to the ZX-24.

I believe that this is possible to do with the ZX-24 but I just do not seem to be able to get my head around how to do it. I have read AN-209, "Task Management" and AN-210 "Sharing Data between Tasks". I confess I do not completely understand what I read, but I have a suspicion the answer is in them somewhere.

Any enlightenment will be appreciated.

Vic
Vic Fraenckel
KC2GUI
windswaytoo ATSIGN gmail DOT com
Don_Kirby
Posts: 341
Joined: 15 October 2006, 3:48 AM
Location: Long Island, New York

Post by Don_Kirby »

Put the code that turns on the buzzer in its' own task. The trick is that, unlike most tasks, it shouldn't loop. It just runs through once and exits, turning on the buzzer output at the beginning, waiting 5 seconds, then turning the buzzer off again.

The next part is figuring out how to trigger that task at the right time. If the computations always take 15 seconds, it should be fairly easy. Right before your start computing the result, call the buzzer task. That task should wait for 10 seconds, then turn on the buzzer for 5, then turn off the buzzer and exit.

-Don
victorf
Posts: 342
Joined: 01 January 2006, 4:08 AM
Location: Schenectady, New York

Post by victorf »

Don,

I suspect I did not make it clear how this puppy will function. The instrument is tracking a moving object. The end of one 15 second period IS the beginning of the next. The observer needs to be warned 5 seconds before the end of each period in order for him/her to be sure the optics's cross hairs are on the tracked object at the time of the measurement. During the "free" period (10 seconds in this case) the calculation/display for the last observation takes place.

Can such a thing be done? If so, how?

Any further enlightenment will be appreciated.

Vic
Vic Fraenckel
KC2GUI
windswaytoo ATSIGN gmail DOT com
Don_Kirby
Posts: 341
Joined: 15 October 2006, 3:48 AM
Location: Long Island, New York

Post by Don_Kirby »

I think I'm understanding correctly. What you need is to have 2 concurrent tasks, one to keep track of the timing and buzzer thing, and another to do the math. Obviously, the math calculations don't take as much time as the you have available (15 seconds in this case).

The code below is an example of what I was describing in my previous post. All of the timing happens in the BuzzerTask. Sub Main() does nothing but call the other tasks. Bear in mind that this simply skeleton code to illustrate the example.

Code: Select all


Private BuzzerTaskStack(1 to 30) as Byte
Private MathTaskStack(1 to 30) as Byte

Sub Main()
Do
	'Check if the BuzzerTask is running
	If NOT(TaskIsValid(BuzzerTaskStack)) Then 
		'If not, start both tasks at the same time
		CallTask "BuzzerTask", BuzzerTaskStack
		CallTask "MathTask", MathTaskStack
	Else 
		'If the BuzzerTask is running, wait till it's done
		Call Sleep(0)
	End If
Loop
End Sub


Private Sub BuzzerTask()
	'Define a pin for the output
	Const BuzzerOutputPin as Byte = C.1
	'Sleep for the first 10 seconds
	Call Sleep(10.0)
	'Turn on the buzzer
	Call PutPin(BuzzerOutputPin, zxOutputHigh)
	'Leave the buzzer on for 5 seconds
	Call Sleep(5.0)
	'Turn off the buzzer
	Call PutPin(BuzzerOutputPin, zxOutputLow)
'End the task
End Sub



Private Sub MathTask() 
	'Do some math calculations here
	Call Sleep(0)
End Sub
There should be error checking in case the MathTask takes longer than expected to complete, as well as a slew of other details that are particular to you specific application.

-Don
Post Reply