#!/usr/bin/perl -w # # remanence - Simulateur de remanente et remplissage continu # $Id: remanence.pl,v 1.1.1.1 2004/06/07 13:59:09 felix Exp $ ### # (C) 2004, Félix Hauri - Felix@F-Hauri.CH # # This is part of demo ``active background'' # must stay in same directory. # # Files: # name access description # # desired R desired temperature # current R/W current temperature # speed R desired speed # lspeed W real speed # tank R/W tank state # die if defined $ENV{'SERVER_NAME'}; use Time::HiRes qw(ualarm); $desired=23; $current=23; $tank=0; $oper=1; $speed=12_000; $pi=4*atan2(1,1); sub sign { my $val=shift; return 0 if $val==0; return $val/abs($val); } sub onestep { # Current temperature follow desired... $_=`cat desired`; chomp; $desired=$1 if /^(\d+.*\d*)$/ and $1 <= 98 and $1 >= 14; $_=`cat current`; chomp; $current=$1 if /^(\d+)$/; my $step = sprintf "%.3f", ($desired-$current)/4; $step = .5 * sign($desired-$current) if abs($step) >.8; $step = .001 * sign($desired-$current) if $step =~ /0.000/ and $current =~ /\.00[12]$|\.99[98]$/; unless ($step =~ /^0.000$/) { $current=sprintf "%.3f", $current+$step; open FH,">current" or die "Can't open current for writting"; print FH $current; close FH; } # Fill than flush from 0 to 100 at speed 0to20k = 0to4% $_=`cat tank`; chomp; $tank=$1 if /^(\d+)$/; $_=`cat speed`; chomp; $speed=$1 if /^(\d+)$/ and $1 >= 0 and 20_000 >= $1; $speed=$speed+500*sin((time % 100)*$pi/50); open FH,">lspeed" or die "Can't open lspeed for writting"; print FH $speed; close FH; unless ($speed==0) { $tank=$tank+$oper*$speed/5000; $oper=-$oper if $tank > 100 or $tank < 0; $tank=0 if $tank < 0; $tank=100 if $tank > 100; $tank=sprintf "%.3f", $tank; open FH,">tank" or die "Can't open tank for writting"; print FH $tank; close FH; } } ualarm 1,1_000_000; $SIG{"ALRM"} ="onestep"; print "Pseudo daemon started\n".'hit "quit" to stop it.'."\n"; for (;;) { exit if <> =~ /^quit$/; };