On The Inside

On The Inside

Thursday, December 29, 2016

Safe Sound C Source Code and Calibration

The C source code that runs on the embedded ATT IOT kit is available as a public project on the mbed.org developer web site. You will want to refer to the source code as I discuss below.

The first thing that you want to setup after forking the project and making sure that it compiles for you is to configure the config.me file as explained in the ATT how to guide for the IOT kit example application. The process is identical for configuring your base URL.  There is an additional feature in this project's config.me, however, if you do not want to test things with the CELL modem initially you may comment out the define line at the top of the file as follows.

//#define USE_CELL_MODEM

This will instruct the compiler to use the ethernet port instead. Make sure that you're connected to a routed connection that can supply a DHCP address. 

The configuration of the rest of safesound is outside the scope of this file.  You'll want to move over to safesound.h to configure the software to work with the hardware as configured.

As mentioned in the first article, the default state of the relays is off, which is problematic if you don't want  a client to simply turn the unit off. Change these first lines to zero to invert the state of the relay sense. You also have to reconfigure the outputs and tim to use normally closed vs normally open. 

// change these to 0 to change the default state of the relays from // off to on
//
#define SOUND_ON_STATE      1
#define SOUND_DIM_STATE     1


You may choose whichever digital ports that you wish to engage the relays. I used D5 through D8. If you are going to use different ports, configure them ehre. 

// these constants define the digital output ports for the relays
//
#define LEFT_IN_RELAY       D5
#define RIGHT_IN_RELAY      D6
#define LEFT_OUT_RELAY      D7
#define RIGHT_OUT_RELAY     D8


The offset and scale for input and output have to match the configuration of input voltmeter hardware that you're using. If you built the project as specified, then your values should be close to what I measured, however, for any kind of accuracy, you will want to measure them yourself. 

The offset is to account for any DC offset that might be present, unless you know that this is an issue for you, leave them set at zero. The scale, on the other hand, must almost certainly be changed. 

To measure the scale, at minimum you will need an AC/DC voltmeter and some way to generate a sine wave. For testing the speaker connections, an amplifier and a source of sine waves, e.g., your computer and a sine wave file will get the job done. Just make sure that the amplifier is all the way down before you start and turn it up slowly. You don't need a lot of power to calibrate this. 

You can get a sine wave here.  I used 1Khz to calibrate. 

It is probably easier to use a sine wave generator if you have one available. Connect your source to one of the speaker jacks and set for a voltage of say 2.5V.  The exact value isn't that critical, anything between 1 and 5 should work. Using your voltmeter, measure the AC voltage present across the input. Now, using a DC voltmeter, measure the voltage present at the input of the A2D converter.  Now double the output of your sine wave source so that it reads, e.g., 5.0V, and again measure the DC voltage on the A2D converter. You should see roughly double the DC voltage that your saw before.

As built, an input of 2.5 VAC yielded 0.06 VDC at the A2D. Doubling the input voltage to 5.0VAC yielded 0.12 VDC.  This is a factor of 41.667 times. So I would set the left and right output scale as shown below. Ideally, you would repeat this procedure for each channel as building practices might yield slightly different behavior in the channels. Also, measure the input and output scaling the same way.  For my board, 3.0 VAC in yielded about 0.5 VDC out, or a factor of six. 

// Offset and scale for each channel for component variations
//
#define LEFT_IN_SCALE       6.0
#define RIGHT_IN_SCALE      6.0
#define LEFT_OUT_SCALE      41.667
#define RIGHT_OUT_SCALE     41.667

#define LEFT_IN_OFFSET      0
#define RIGHT_IN_OFFSET     0
#define LEFT_OUT_OFFSET     0
#define RIGHT_OUT_OFFSET    0

As with the digital ports, make sure that these number match the ports chosen for the function indicated.

// these constants define the analog input ports for the voltmeters
//
#define LEFT_IN_A2D         0
#define RIGHT_IN_A2D        1
#define LEFT_OUT_A2D        2
#define RIGHT_OUT_A2D       3


Note that, as set, the A2D converter has a full scale reading of 1.024 volts. Built as shown this means that the maximum output allowed for a full scale reading is approximately 42VAC which corresponds to about 225 Watts at 8 ohms and about 450 Watts at 4 ohms. If you need to measure higher power, and your relays can handle the power, it is a simple matter to increase the input resistance on the voltmeter circuit.

#define A2D_GAIN            GAIN_FOUR
#define A2D_FULL_SCALE      1.024       // corresponds to gain, define manually
#define A2D_MAX_POSITIVE    32767


The code includes a modified Arduino driver for the ADS1015 and ADS1115 four channel A2D converters. These I2C boards provide reliable analog input that can be more easily isolated from the remainder of the circuits than the built in converters. They are also more sensitive. The driver can be found in ADS1015.h and ADS1015.cpp.  While it's possible to build this circuit using the built in A2D converters, I found the ADS1115 to be far more reliable. I also found that the driver conversion times for the A2D converter were too short. I increased them to 100mS in the driver. This works fine for the sensor reading rate of once every five seconds. If you want to increase the rate to more than about once a second, you may have to adjust the conversion delay in ADS1015.cpp. 



No comments:

Post a Comment