Device-USB-PCSensor-HidTEMPer

 view release on metacpan or  search on metacpan

lib/Device/USB/PCSensor/HidTEMPer/NTC/External.pm  view on Meta::CPAN

    # Initialize the gain, this will be automatically adjusted later on.
    $self->{gain}   = INITIAL_GAIN;
    $self->_write_gain( $self->{gain} );
    
    bless $self, $class;
    return $self;
}

=item * celsius()

Returns the current temperature from the device in celsius degrees.

=cut

sub celsius
{
    my $self        = shift;
    
    my @data        = ();
    my $counter     = 0;
    my $volt        = 0;
    my $key         = 0;
    my $temperature = 0;
    
    # Command 0x41 will return the following 8 byte result, repeated 4 times.
    # Position 0: Part one of the float number
    # Position 1: Part two of the float number
    # Position 2: Part three of the float number
    # Position 3: unused
    # Position 4: unused
    # Position 5: unused
    # Position 6: unused
    # Position 7: unused
    
    # This device may return 255*8 until it is ready for use.
    @data        = $self->{unit}->_read( 0x41 );
    $counter     = 0;

    READ: until ( $counter > 20 ){
                      next READ if $data[0] == 0xFF 
                                    && $data[1] == 0xFF
                                    && $data[2] == 0xFF;
                                    
                      # Caluculate returned reading
                      $volt = ( ( ( $data[0]-128 ) + ( $data[1] / 256 )  ) / 52.032520325203252 ) / $self->{gain};
                      
                      last READ if $self->_new_reading_needed( $volt ) == 0;
                  }continue{
                      $counter++;
                      sleep 0.2;
                      @data = $self->{unit}->_read( 0x41 );
                  }
    
    croak 'Invalid readings returned' if $counter >= 21;
    
    # Calculate key
    $key = $self->_volt_7705_calibration( $volt );
    
=pod

The formula used to calculate value based on a calibrated key value is
created using the Eureqa tool from Cornell Computational Synthesis Lab,
http://ccsl.mae.cornell.edu/eureqa.

Resulting in the use of this formula instead of the provided number list:
f(y)=66.7348/(66.7275/(67.8088 - 9.70353*log(0.000251309 + y*y)) - 0.21651)

If you find another formula that is more accurate please drop me a line. 
The data used can be found in the source code of this file.

=cut 

    $temperature = 66.7348 
                  / ( 66.7275 
                      / ( 67.8088 
                          - 
                          9.70353
                          * 
                          log( 0.000251309 
                               + ( $key
                                   * $key ) 
                          )
                    ) 
                    - 
                    0.21651 
                  );

    return $temperature;
}

# Calculate Volt7705Calibration
sub _volt_7705_calibration
{
    my $self        = shift;
    my ( $volt )    = @_;
    my $reference   = undef;
    
    # Select the correct values needed
    if( $volt <= 0.0010888 ){ 
        return -0.000334633; 
    }elsif( $volt <= 0.0012803 ){ 
        $reference = CALIBRATION_VALUES->[0]; 
    }elsif( $volt <= 0.0017002 ){
        $reference = CALIBRATION_VALUES->[1]; 
    }elsif( $volt <= 0.002666 ){ 
        $reference = CALIBRATION_VALUES->[2]; 
    }elsif( $volt <= 0.00522 ){
        $reference = CALIBRATION_VALUES->[3];
    }elsif( $volt <= 0.0149 ){ 
        $reference = CALIBRATION_VALUES->[4]; 
    }elsif( $volt <= 0.04683 ){ 
        $reference = CALIBRATION_VALUES->[5]; 
    }elsif( $volt <= 0.21342 ){
        $reference = CALIBRATION_VALUES->[6]; 
    }elsif( $volt <= 0.36914 ){ 
        $reference = CALIBRATION_VALUES->[7]; 
    }elsif( $volt <= 0.44121 ){ 
        $reference = CALIBRATION_VALUES->[8];
    }elsif( $volt <= 0.65351 ){
        $reference = CALIBRATION_VALUES->[9]; 
    }elsif( $volt <= 0.92445 ){ 
        $reference = CALIBRATION_VALUES->[10]; 
    }elsif( $volt <= 1.08022 ){
        $reference = CALIBRATION_VALUES->[11]; 
    }elsif( $volt <= 1.8745 ){
        $reference = CALIBRATION_VALUES->[12]; 
    }elsif( $volt <= 1.9943 ){
        $reference = CALIBRATION_VALUES->[13]; 



( run in 1.588 second using v1.01-cache-2.11-cpan-5a3173703d6 )