Clear Timer on Every Other Capture and track location

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hey Folks....more trouble for me....I'm using an mega8535 and I'm having trouble with the 16bit timer1. I have a sensor that is located off center on an inverted pendulum. I need to reset the timer every other time the timer1 capture interupt fires. That part is the easy part, I just XOR a bit each time and clear timer1 when it equals one. The hard part is I also need to know where the pendulum is! I tried comparing times between the first couple swings after the mega8535 is reset, but sometimes it thinks its on the left, sometimes it thinks its on the right ... I've spent all day on this problem. :roll: My timer1 capture interupt subroutine is below. I'd appreciate any help!
Thanks,
Jason

TIM1_CAPT:
	sbrs Flag, 7					; Check if Time1 already Saved
	rjmp Save_Time1
	sbrs Flag, 6					; Check if Time2 already Saved
	rjmp Save_Time2

	rjmp Flip_Flop


Save_Time1:
	in Time1L, TCNT1L					;
	in Time1H, TCNT1H					; Save Time1 and set flag saying we did so
	sbr Flag, 0x80	
	rjmp Reset_Timer

Save_Time2:
	in Time2L, TCNT1L					;
	in Time2H, TCNT1H					; Save Time1 and set flag saying we did so
	sbr Flag, 0x40	

	cp Time2L, Time1L
	cpc Time2H, Time2H
	brsh Exit_Timer_Capture

Flip_Flop:
	ldi Temp, 0x01
	eor Flag, Temp						; Switch bit 0 in Flag 

	sbrs Flag, 0						; skip if bit 0 is set
	rjmp Exit_Timer_Capture				; else jump to Done

; CODE HERE TO RUN EVERY OTHER TIME

Reset_Timer:
	ldi Temp, 0x00						; Reset Timer;
	out TCNT1H, Temp					;
	out TCNT1L, Temp					;

Exit_Timer_Capture:				;
	reti

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It seems to me like you need two sensors, offset equal distance from center (both sides of center). From the two sensors, you can determine which side the pendulum is on and which direction it is going (with timing information).

Imagine the possible conditions of the two sensors.

Sensor "Left" active
Sensor "Right" active
no sensors active "None"

Write a state diagram.

State------------Transition Condition-----------------New State----------Pendelum
Initial-------------Left Active---------------------------Left-----------------start on left side
Initial-------------Right Active--------------------------Right---------------start on right side
Left--------------None Active--------------------------Left None---------swing either direction
Left None-------Left Active---------------------------Left-----------------swing L-R
Left None-------Right Active--------------------------Right---------------swing L-R
Right------------None Active--------------------------Right None--------swing either direction
Right None-----Right Active--------------------------Right---------------swing R-L
Right None-----Left Active---------------------------Left-----------------swing R-L

Using your timers, you can determine pendelum velocity and frequency. With this information you can determine the pendelum direction in the ambiguous states.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,
Thanks for the reply. I am unfortunetly unable to have two sensors and I'm pretty sure it can be done with one sesnsor because its offset from the side and the pendulum swing is steady (rock solid). If it can't, I still don't see it. Why can't I just time the two swings to find out where I am?

Thanks,
Jason

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Make sure you throw away your first value because it will power on to an undefined state. You cannot have a defined state until at least one 'trigger' has been recorded

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Don't ignore alternate captures at the start. Time first to second and second to third. One will be less than the other. If your sensor is offset, you should be getting alternate long and short intervals - which is why you have to take every second sample in the first place. :)

C. H.
-------------------------------------------------------------------
It's only waste if you don't use it!