' ***********************************************************************
' *                    < HOLOSOUND - Hardware V3.0 >                    *
' *                               <DIANA>                               *
' *                  Hybrid computer section front-end                  *
' *             Source code for Basic Stamp 1 microcontrollers          *
' *          used for acceleration derivation in channels X,Y,Z         *
' *                        ADC-channels 12, 13, 14                      *
' *                      by Dr.Godfried-Willem Raes                     *
' *                         Filename: ACC2PWM.BS1                       *
' ***********************************************************************
' 04.09.1995: first design and testing session
' 05.09.1995: bug: returns only negative acceleration values...
'             problem solved. Code bug.
' 05.09.1995: The problem is in the fluctuations of the velocity
'             acquisition time. This makes the aperture-time very
'             unequal although related to to values read.
'             We must find a mathematical solution  for this...
' 06.09.1995: Mathematical considerations:
'             acceleration = delta v / delta t
'             delta t = programlooptime + last period time
'             v0 = 1/previous period time
'             v1 = 1/last period time
'             delta v = v0 - v1 = f0 - f1
'             (velocity is linearly proportional to doppler frequency)
'             This program fills about half of the memory space 
'             (129 bytes of 255 available bytes)
' 22.09.1995: file renamed *.BS1 to avoid confusion with code written for
'             Stamp2 pics.
' 23.09.1995: changes in pin layout in function of PC-board design for
'             <DIANA>: pin 5= pwm output, pin 0= pulse input
' 28.09.1995: Algo2 kode ontworpen: df/dt kode.
'             Het is mogelijk een van de vrije inputs te gebruiken als
'             selektor voor het na reset te gebruiken algoritme.
' 29.10.1995: <DIANA> board populated.
'             BSLOAD must run under DOS, not in a dos Window under WFWIN!!!
' 31.10.1995: Lab tests.
'             Code changed to use only positive pulse-lenghts
'             Previous code renamed A2P-OLD.BS1
'             Tested O.K. - pretty fast now!
'             Previous Stamps in  Diana channel Z probably blown up...
'             (caused by fucking Windows?)
 
' Written to examine the use for vectorial acceleration
' measurement in invisible radar instruments.
' Three BASIC Stamps serve as acceleration to pwm converters.
' Their outputs are DC-voltages to be fed to ADC-converter channels. 
' They accept pulse input through their pin 0, and output pwm-signals
' from their pin 5's. Resolution is <= 7 bits.
' Pin 5 should be connected to an RC-circuit, for integration:
'              4.7kOhm- 0.47mF gives 2.2 ms RC time   Icmax=1mA
'              10kOhm - 470nF                         Used in <DIANA>
'              10kOhm - 1 mF gives 10ms RC-time
'              10kOhm - 10mF gives an 0.1s RC-time    Icmax=0.5mA
'             100kOhm - 1 mF gives the same result.   Icmax=50microamps
' The dc-signal should be buffered. The DC range is 0-5V
' The center-zero value is 2.5Volt        
' Conversion speed is a function of the input signal!

' CODE-LIST:                    ' timing ' comments
'                               ' 0.5ms/instruction
Init:
        input 0                 ' 1ms    ' input pin for puls-measument
        input 1                 '        ' grounded
        input 2                 ' 	 ' not used
        input 3                 ' 	 ' not used
        input 4                 '        ' grounded
        input 5                 ' 1ms    ' pwm output pin - for drift reduction   
	input 6                 ' 1ms    ' not used
	input 7                 ' 1ms    ' not used  - ?Algo select pin (driven by mP)

        if pin7=0 then begin  ' select first algorithm
       ' if pin7=1 then algo2  ' select second algorithm
Begin:
	b0 = 27                 ' 1.5ms  ' estimated loop-time for program 
                                         ' expressed in ms
					 ' the units used will be ms only.
				'          perform a first conversion: 
                                '          positive pulse only
	pulsin 0,1,w1           ' 2ms+w1*100  
                                '          w1 = 0 -> $FFFF * 10 microsec.
	w1 = w1 / 100           ' 2.5ms    w1 = 0 -> 655 ms 

Frq:                            ' 0.5ms  ' perform a conversion: half period
	pulsin 0,1,w2           ' 2ms+w2*100   
                                '          w1 must be preserved, so dont use
                                '          this variable
	w2 = w2 / 100           ' 2.5ms    w2 is now expressed in ms
        IF w2 < w1 THEN Versnel ' 3ms    
                                ' = 5ms (frq sektie)

Vertraag:                       ' 0.5ms
	w3 = w2 - w1            ' 2.5ms  ' verschil der periodes steeds 
					 ' positief : range= [0] 1-655ms
	w4 = b0 + w2            ' 2.5ms  ' meettijd inklusief pulstijd 
	w5 = w3 * 1000 / w4 max 128 ' 4.5ms  ' bereken de versnelling (neg)
	b1 = 128 - w5           ' 2.5ms
	pwm 5,b1,1              ' 7ms = 2+5ms  ' output a 0->2.5V value
	w1 = w2                 ' 1.5ms
	goto Frq                ' 1ms
				' =======
				' = 22ms (Vertraag sektie)
				' => lustijd 27ms
Versnel:                        ' 0.5ms
	w3 = w1 - w2            ' 2.5ms  ' verschil der frekwenties steeds 
					 ' positief
	w4 = b0 + w2            ' 2.5ms
	w5 = w3 * 1000 / w4     ' 3.5ms  ' bereken de versnelling (pos)
	b1 = 128 + w5 max 255   ' 3.5ms
	pwm 5,b1,1              ' 7ms = 2+5ms  ' output a 2.5->5V Value
	w1 = w2                 ' 1.5ms
	goto Frq                ' 1ms
				' =======
				' = 22ms (Versnel sektie)
				' => lustijd 27ms

' Kode (experimenteel) voor uiterst snelle versnellingsmetingen:
' Hier wordt als basis voor de versnellingsmeting uitgegaan van de 
' assymmetrie van een enkele periode.
' deze kode zou bijzonder geschikt moeten zijn voor het detekteren van
' botsingen en overgangen van stilstand naar beweging.
' Voor meting van versnellingswaarden, is het gedrag erg onlineair!
' Immers de snelheidsverandering moet al minstens 1:2 belopen eer zij
' door deze kode zal worden herkend.
Algo2:
       pulsin 0,1,w1
       pulsin 0,0,w2
       If w1 > w2 THEN versnel2
Vertraag2:
       w3= w2/w1 MAX 637        ' w3 >=1 !
       b1 = w3 / 5              ' scale to 0-127
       b1= 128 - b1             ' 128 -> 0 (negative bipolar)
       pwm 5,b1,1
       goto Algo2
Versnel2:
       w3 = w1/w2 MAX 637
       b1 = w3 / 5              ' scale to 0-127
       b1 = 128 + b1            ' 128 -> 255 (positive, bipolar)
       pwm 5,b1,1
       goto Algo2

       bsave