a sensor control - demo

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

a sensor control - demo

Post by MrSwiss »

Hi all,

this demo shows a type, to handle sensor data & status ...

It shows a supposed Water-Heater operating on data which is:
  • 1) user supplied (a const in the demo), setting target temperature etc.
    2) a generated random value (here, supposed sensor reading)
    3) generated delay times (for further heating periods)
What is NOT taken into account:
- a regulator, to control *overshooting*, which is common, to such scenarios
- a ASCII to ANSI error, currently in the code (on WIN, unimportant detail here)

demo code:

Code: Select all

' sensor_type-test1.bas -- 2018-06-21, MrSwiss
'
' compile: -s console
'
' type definition start
Type sensor     ' a type recording sensor data & status vs. a pre-set target value
  Private:      ' NO directt user access
    As Single   targ, curr, diff    ' internal variables
    As Boolean  done = FALSE        ' status variable
    As UByte    res(1 To 3)         ' manual padding (unused currently, kept reserve)
  Public:       ' user interface (aka: abstaction)
    Declare Constructor(ByVal As Single, ByVal As Single=22.0)      ' ctor
    Declare Function scurr(ByVal As Single) ByRef As Const Boolean  ' setter (new curr), return: status
    Declare Function gcurr() ByRef As Const Single                  ' getter (curr value)
    Declare Function gdiff() ByRef As Const Single                  ' getter (diff value)
    Declare Function gdone() ByRef As Const Boolean                 ' getter (status: done FALSE/TRUE)
End Type

Constructor sensor(ByVal nt As Single, ByVal nc As Single=22.0)
    This.targ = nt          ' initialize type's variables
    This.curr = nc
    This.diff = nt - nc
End Constructor
Function sensor.scurr(ByVal nc As Single) ByRef As Const Boolean
    This.curr = nc          ' set new current value
    If nc = This.targ Then  ' are we done yet?
        This.diff = 0.0     ' yes, set vars. accordingly
        This.done = TRUE
    Else                    ' not done, just update 'diff'
        This.diff = THis.targ - This.curr
    End If
    Return This.done        ' return status of 'done'
End Function
Function sensor.gcurr() ByRef As Const Single
    Return This.curr        ' return current value
End Function
Function sensor.gdiff() ByRef As Const Single
    Return This.diff        ' return current difference
End Function
Function sensor.gdone() ByRef As Const Boolean
    Return This.done        ' return current status
End Function
' type definition end


' generate a random single from: within a given range
#Define SRange(l, h)    ( CSng(Rnd * ((h) - (l)) + (l)) )

'prepare randomizing stuff ...
Randomize( , 5)
For i As UInteger = 1 To 50
    Rnd() : Rnd() : Rnd() : Rnd() : Rnd() : Rnd()
Next

' ===== MAIN =====
Width 80, 25                            ' standard console
Const As Single targtemp = 80.0         ' instead of user input (pre-set target)
Dim As Double   ts, te                  ' timer variables
Var pt = New sensor(targtemp, 25.0)     ' default ctor call (override default on second parameter)

Do
    If pt->gdone Then Exit do           ' target temp. reached? yes --> quit
    If pt->gdiff > 0.0 Then             ' get new difference
        ts = Timer                      ' init/reinit timer start
        Select Case pt->gdiff           ' set new time to heat up (heating element)
            Case Is < 10                ' close to target: do fine adjustment
                Select Case As Const CULng(pt->gdiff)   ' using a conversion to a imt type
                    Case 0 : te = ts + 0.25 ' allmost there, add minimal time to go
                    Case 1 : te = ts + 1
                    Case 2 : te = ts + 2    ' quite close
                    Case 3 : te = ts + 3
                    Case 4 : te = ts + 4
                    Case 5 : te = ts + 5    ' some distance to go
                    Case 6 : te = ts + 6
                    Case 7 : te = ts + 7    ' quite a distance to go
                    Case 8 : te = ts + 8
                    Case 9 : te = ts + 9    ' still quite far away
                End Select
            Case Is < 20 : te = ts + 10 ' different initial heating periods, _
            Case Is < 30 : te = ts + 15 ' depending on temp. difference
            Case Is < 40 : te = ts + 20
            Case Else : te = ts + 30    ' aka: >= 40
        End Select
        Print "please wait for: "; Str(CSng(te - ts));  ' keep user informed
        Print " Seconds -- current temp. "; Str(pt->gcurr())
        While Timer < te : Sleep(10, 1) : Wend  ' heating duration wait-loop
    End If
    If CsrLin = 25 Then Cls             ' clear screen if bottom line reached
    pt->scurr(SRange(pt->gcurr, targtemp))  ' setter of curr (new sensor value)
Loop
Print
Print "Target temp. "; Str(targtemp); " °C reached! press a key to exit ... ";
Sleep
' clean up: release allocated memory
Delete pt                               ' indirect defaut dtor call
' ===== END-MAIN =====  ' ----- EOF -----
Results from two run's made with the demo ...
please wait for: 30 Seconds -- current temp. 25
please wait for: 30 Seconds -- current temp. 35.75963
please wait for: 15 Seconds -- current temp. 55.08641
please wait for: 10 Seconds -- current temp. 65.05462
please wait for: 4 Seconds -- current temp. 75.5601
please wait for: 3 Seconds -- current temp. 77.13853
please wait for: 1 Seconds -- current temp. 79.06241
please wait for: 1 Seconds -- current temp. 79.23209
please wait for: 0.25 Seconds -- current temp. 79.55796
please wait for: 0.25 Seconds -- current temp. 79.87233
please wait for: 0.25 Seconds -- current temp. 79.96168
please wait for: 0.25 Seconds -- current temp. 79.97746
please wait for: 0.25 Seconds -- current temp. 79.98941
please wait for: 0.25 Seconds -- current temp. 79.99931
please wait for: 0.25 Seconds -- current temp. 79.99969
please wait for: 0.25 Seconds -- current temp. 79.99989
please wait for: 0.25 Seconds -- current temp. 79.99992
please wait for: 0.25 Seconds -- current temp. 79.99999
please wait for: 0.25 Seconds -- current temp. 79.99999
please wait for: 0.25 Seconds -- current temp. 79.99999
please wait for: 0.25 Seconds -- current temp. 79.99999
please wait for: 0.25 Seconds -- current temp. 79.99999

Target temp. 80 ¦C reached! press a key to exit ...

please wait for: 30 Seconds -- current temp. 25
please wait for: 30 Seconds -- current temp. 35.53243
please wait for: 10 Seconds -- current temp. 69.83836
please wait for: 3 Seconds -- current temp. 77.45357
please wait for: 0.25 Seconds -- current temp. 79.66068
please wait for: 0.25 Seconds -- current temp. 79.85067
please wait for: 0.25 Seconds -- current temp. 79.98853
please wait for: 0.25 Seconds -- current temp. 79.99329
please wait for: 0.25 Seconds -- current temp. 79.99638
please wait for: 0.25 Seconds -- current temp. 79.99653
please wait for: 0.25 Seconds -- current temp. 79.99696
please wait for: 0.25 Seconds -- current temp. 79.9983
please wait for: 0.25 Seconds -- current temp. 79.99844
please wait for: 0.25 Seconds -- current temp. 79.99899
please wait for: 0.25 Seconds -- current temp. 79.99975
please wait for: 0.25 Seconds -- current temp. 79.99975
please wait for: 0.25 Seconds -- current temp. 79.99994

Target temp. 80 ¦C reached! press a key to exit ...
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: a sensor control - demo

Post by MrSwiss »

Just seen the Byref discussion at: WIKI improvements
Above example/demo code uses ByRef returns, which may be a starter ...
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: a sensor control - demo

Post by fxm »

In your code, I see no point in returning by reference.
So not very attractive as an example!
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: a sensor control - demo

Post by MrSwiss »

ByRef returns from Functions, are "faster" than ByVal ... (no copy op. needed)

Note: it is important to use "Const" qualifier, to "close a open back-door",
which would allow, modification of the "type internal variable"!
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: a sensor control - demo

Post by fxm »

MrSwiss wrote:ByRef returns from Functions, are "faster" than ByVal ... (no copy op. needed)
Yes for a big variable, but insignificant for a simple numerical pre-defined variable as Byte, Integer, Single...
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: a sensor control - demo

Post by MrSwiss »

I'm sure that you don't expect me to agree, with that nonsense statement, of yours:
- saving resources (as much as possible)
- gaining all possible speed
that's to my mind, a professional attitude, all else is considered: amateurish ...
(You've lost the attribute "being a reference" for myself, some time ago, because
you don't sufficiently care, about the small things, which may make the difference,
in the end.)
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: a sensor control - demo

Post by fxm »

Since the discussion is degenerating, I prefer to stop there.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: a sensor control - demo

Post by MrSwiss »

But you have to have, as always ... the last word.

Remember: you've started with, imho, witless criticism (opposite of: constructive ...)
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: a sensor control - demo

Post by fxm »

MrSwiss wrote:Just seen the Byref discussion at: WIKI improvements
Above example/demo code uses ByRef returns, which may be a starter ...
fxm wrote:In your code, I see no point in returning by reference.
So not very attractive as an example!
.....
But since you master this subject and are constructive, then write this article rather than me.
coderJeff
Site Admin
Posts: 4323
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: a sensor control - demo

Post by coderJeff »

You guys... No joking, I find myself looking up the history of national relations between france & switzerland...
MrSwiss wrote:ByRef returns from Functions, are "faster" than ByVal ... (no copy op. needed)
A function returning byref is effectively returning a pointer. To use the value it points to in an expression, the contents of memory where the pointer references, has to be loaded. A function returning byval is giving back the value in a register (or fpu stack on 32-bit for floats), and can be used directly in the next operation. If you never use the return value, like in scurr(), probably doesn't matter either way.

If you actually want a pointer, like you are passing as a byref parameter, on 32-bit the pointer is already loaded in a register and can get pushed to the stack. On 64-bit args are passed in registers, though might have swap registers around.

I think byref function return, passed to byval parameter probably worst case.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: a sensor control - demo

Post by MrSwiss »

fxm wrote:But since you master this subject and are constructive, then write this article rather than me.
Definitely not, at this point in time, because of the unresolved issues (in current "official" FBC 1.05.0):
  • NOTE: The arrays of references and the non-static reference fields for UDT are not supported yet.
  • until the current problems are solved, aka: FBC ver. 1.06.0 becomes "officially released"
Btw: thanks to coderJeff, who is currently addressing the issues, related to ByRef Variables.

Of equal importance, imho, is Bug Report #828 "32bit Integer/Long mangling reversed",
which should be solved, before ver. 1.06.0 becomes "officially released" ...
(Allowing the same source, to be used "without modification", in 32/64 bit FBC.)
coderJeff wrote:No joking, I find myself looking up the history of national relations between france & switzerland...
Seems to me, that you are looking in the wong direction (nothing to do with: national relations).
It's more the attitude, that's different, french "laisser faire" vs. swiss "precision".
coderJeff
Site Admin
Posts: 4323
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: a sensor control - demo

Post by coderJeff »

MrSwiss wrote:Bug Report #828 "32bit Integer/Long mangling reversed"
With help from dkl, first step of this change is ready. Breaks binary compatibility though, so there will be no good time to merge. Least worst time will be right before a 1.06 release.
deltarho[1859]
Posts: 4305
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: a sensor control - demo

Post by deltarho[1859] »

I should not tell this story but I will anyway. My girlfriend and I rolled up at a campsite just outside of Marseille. The chap in front of us was being told that they were full. We were just about to walk away and the camp owner said: "Can I help you?". I said, "No, it's OK you are full". "You are English?" said the camp owner. "Yes" I said to which he responded with "In that case, we are not full". I pointed at the guy leaving and the camp owner said: "He's German". I was then told that Marseille had a bit of history with Germany and the folk in the area had a long memory.

I should add that was in the early eighties so I hope things have changed since then. France and Germany and now 'best pals' in the EU so perhaps things have changed. Of course, some things don't change until older folk 'shuffle off this mortal coil'.

Having said that a guy was forever ribbing me about being a Yorkshireman - he was from Lancashire. So what, you may say. Well, back in 1455, for thirty years, the two counties were at loggerheads. The war ceased when the male line of both houses was eliminated. That was over 500 hundred years ago and I was getting ribbed!
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: a sensor control - demo

Post by MrSwiss »

Similar thing happend to me (late 70ties), in Oslo/Norway.
(I couldn't speak proper English then.)
Asking a 'local' in brocken English, about the public transport (we needed to use), to get
to the Camp-Site (I knew the name, because I've been there before), the bloke then asked:
"Are you Germans?". To which I've answered: "No, we're Swiss, speaking SwissGerman", after
that, he replied: "In this case, I'll speak German with you" ... (remnants of WW2, I'd guess).
His German was excellent, btw ...
Post Reply