Page 1 of 2

OpenXSensor @8MHz

Posted: Mon Jun 16, 2014 12:54 pm
by RaptorSense
I have expanded ‘Aserial.cpp’ a little to make it compatible with the 8MHz boards. It’s working on a 3.3v ProMini board without glitches.

The division factor for the delay_us(x) macro is simply expanded. (Isn’t it simpler to throw away the FOSC value and use F_CPU instead?)

Code: Select all

// Sinan -Removed: #define FOSC 16000000UL // internal clock of 16 mhz
#if F_CPU   == 20000000L  // for the 20 MHz clock on rare Arduino boards
  #define FOSC 20000000UL 
#elif F_CPU == 16000000L  // for the 16 MHz clock on most Arduino boards
  #define FOSC 16000000UL 
#elif F_CPU == 8000000L   // for the 8 MHz internal clock
  #define FOSC 8000000UL 
#else
  #error Unsupported clock speed
#endif
The tick counts depending on the clock speed and the selected baudrate:

Code: Select all

#ifdef FRSKY_SPORT
  // This section chooses the correct timer values for the chosen baudrate depending on the clock speed.
  // 57600 = Desired baudrate pour le Sport Interface = 17 micro sec per bit.
  #if F_CPU == 20000000L   // 20MHz clock 
    // Sinan: Not tested                                                     
    #define TICKS2COUNT         348  // Ticks between two bits.
    #define TICKS2WAITONE       348  // Wait one bit period.
    #define TICKS2WAITONE_HALF  520	 // Wait one and a half bit period.
  #elif F_CPU == 16000000L  // 16MHz clock                                                  
    #define TICKS2COUNT         278  // Ticks between two bits.
    #define TICKS2WAITONE       278  // Wait one bit period.
    #define TICKS2WAITONE_HALF  416	 // Wait one and a half bit period.
  #elif F_CPU == 8000000L   // 8MHz clock
    // Assumes a 8MHz clock                                                   
    #define TICKS2COUNT         139  // Ticks between two bits.
    #define TICKS2WAITONE       139  // Wait one bit period.
    #define TICKS2WAITONE_HALF  208	 // Wait one and a half bit period.
  #else
    #error Unsupported clock speed
  #endif
#else // FRSKY HUB
  // This section chooses the correct timer values for the chosen baudrate depending on the clock speed.
  // 9600   =  Desired baudrate for FrSky hub
  #if F_CPU == 20000000L     // 20MHz clock                                                  
    // Sinan: Not tested
    #define TICKS2COUNT         (348*6)  // Ticks between two bits.
    #define TICKS2WAITONE       (348*6)  // Wait one bit period.
    #define TICKS2WAITONE_HALF  (520*6)	 // Wait one and a half bit period.
  #elif F_CPU == 16000000L   // 16MHz clock                                                  
    #define TICKS2COUNT         (278*6)  // Ticks between two bits.
    #define TICKS2WAITONE       (278*6)  // Wait one bit period.
    #define TICKS2WAITONE_HALF  (416*6)	 // Wait one and a half bit period.
  #elif F_CPU == 8000000L    // 8MHz clock                                                   
    #define TICKS2COUNT         (139*6)  // Ticks between two bits.
    #define TICKS2WAITONE       (139*6)  // Wait one bit period.
    #define TICKS2WAITONE_HALF  (208*6)	 // Wait one and a half bit period.
  #else
    #error Unsupported clock speed
  #endif
#endif
Inside the init() function there is a section to determine the prescaling factors for the AD converter(?). I am not using the analogue ports and I don't know what to do with these thus left this part untouched.

Code: Select all

void init()
{
  // Timer1
  TIMSK1 &= ~( 1<< OCIE1A ) ;
  TCCR1A = 0x00 ;    //Init.
  TCCR1B = 0xC1 ;    // I/p noise cancel, rising edge, Clock/1

// TODO: this code should use F_CPU to determine the prescale factor.
#if defined(ADCSRA)
  // set a2d prescale factor to 128
  // 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range.
  // XXX: this will not work properly for other clock speeds, and
  // this code should use F_CPU to determine the prescale factor.
  sbi(ADCSRA, ADPS2);
  sbi(ADCSRA, ADPS1);
  sbi(ADCSRA, ADPS0);

  // enable a2d conversions
  sbi(ADCSRA, ADEN);
#endif
Please be careful by wiring the 3.3v boards. Most USB-to-Serial adapters supply 5 volt at the VCC pin. In case of using 3.3v sensor boards, 5v has to be supplied through the VRaw pin.

Cheers, Sinan

Re: OpenXSensor @8MHz

Posted: Fri Jun 27, 2014 4:32 pm
by davx
Hi,

I'm really interested in using an OXS at 3.3v - 8Mhz in a dlg glider powered by a single lipo.

Could this setting be included in the oxs_config.h or could it be automatic ?

Thanks.

Re: OpenXSensor @8MHz

Posted: Tue Jul 01, 2014 10:55 am
by davx
Hi,

I've pushed a little the work that RaptorSense has already done in "Aserial.cpp" and tried to adapt AD converter prescale factor to different clock speed:

Code: Select all

void init()
{
  // Timer1
        TIMSK1 &= ~( 1<< OCIE1A ) ;
  TCCR1A = 0x00 ;    //Init.
  TCCR1B = 0xC1 ;    // I/p noise cancel, rising edge, Clock/1

#if defined(ADCSRA)
  #if ( F_CPU == 20000000L ) || ( F_CPU == 16000000L )   // 20 MHz or 16 MHz clock
        // set a2d prescale factor to 128
        // 20 MHz / 128 = 156.25 KHz
        // 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range.
        // XXX: this will not work properly for other clock speeds, and          
        // this code should use F_CPU to determine the prescale factor.
        sbi(ADCSRA, ADPS2);
        sbi(ADCSRA, ADPS1);
        sbi(ADCSRA, ADPS0);
  #elif F_CPU == 8000000L    // 8MHz clock        
        // set a2d prescale factor to 64
        // 8 MHz / 64 = 125 KHz, inside the desired 50-200 KHz range.
        sbi(ADCSRA, ADPS2);
        sbi(ADCSRA, ADPS1);
        cbi(ADCSRA, ADPS0);
  #else
    #error Unsupported clock speed
  #endif
  
        // enable a2d conversions
        sbi(ADCSRA, ADEN);
#endif
Are there modifications needed elsewhere in the code to let us use an 8MHz arduino ?

Could mstrens or Mike comment please ?

Tchao

Re: OpenXSensor @8MHz

Posted: Tue Jul 01, 2014 7:02 pm
by mstrens
1) I think that those 2 instructions have to be modified too depending on the clock speed.
#define INTERRUPT_EXEC_CYCL 90 // Cycles to execute interrupt routines from interrupt.
#define INTERRUPT_EARLY_BIAS 32 // Cycles to allow of other interrupts.
So for 8 mhz, 90 should be 45 and 32 should be 16.

2) I think that function micros() has to be adapted.
Timer1 use a prescaler = 1; so it runs normally at 16Mhz.
Timer1 is used to manage the serial communication with Rx based on 3 parameters (that you have changed) + the 2 parameter here above; so it should be ok.
But take care that Timer 1 is used too to calculate the functions millis() and micros(). OXS do not use the native arduino functions.
So those functions should also be adapted to take care that Timer1 runs at a different speed.

3) Please note that OXS performs a lot of calculations when all options are activated (vario,voltage, current, ppm, reset).
The vario calculations take a lot of time. You should test if there is no issue when CPU runs at 8 mhz.

Please note too that some data (like voltage, current, ...) are made available for transmission every X millisec.
The transmitted values are often an average of several measurements.
If CPU is running at 8 mhz instead of 16 mhz the averages will be based on less measurements. This could generate a little more variations in the transmitted data.

In some previous versions of OXS, some data where made available after a fix number of measurements. This means that reducing the CPU frequency could delay the availability of some data but I think (still not 100% certain) that this type of process has every where been replaced by calculations every X millisec.

I hope this can help.

Remark: Are you sure that you really need to use a 8 mhz version when using a single lipo?
did you try to run an arduino pro mini 5 volt at a lower voltage (between 3.5 and 4.2 volt = 1 lipo)? Even if it is outside the specifications, it should perhaps still work fine.

Re: OpenXSensor @8MHz

Posted: Tue Jul 01, 2014 8:26 pm
by davx
Thanks for your answers, I will try to get it to work.

I don't need all the functionalities of oxs, vario and some temp sensors, perhaps voltage...

Yes, there is a chance that arduino 16MHz could run with 3.5v but I would prefer to give it a stable 3.3v and use it as a reference for analog ports.

Re: OpenXSensor @8MHz

Posted: Tue Jul 01, 2014 8:58 pm
by mstrens
I think that in micros() instruction
elapsed >>= 4 ;
should become
elapsed >>= 3 ;

If you use a RPM sensor, then probably you have also to change
eventTime = TotalMicros + ( elapsed >> 4 ) ;
in
eventTime = TotalMicros + ( elapsed >> 3 ) ;

Re: OpenXSensor @8MHz

Posted: Tue Jul 01, 2014 9:00 pm
by MikeB
You need 3.78 volts to guarantee 16 MHz operation. At 3.3 volts, you can run up to 13.3MHz.

Mike.

Re: OpenXSensor @8MHz

Posted: Tue Jul 01, 2014 9:20 pm
by davx
@mstrens,
If 8MHz could be sufficient for oxs to operate normally, I think we should take the opportunity to make oxs compatible with those arduino pro mini 3.3v 8MHz with some clean code ;)

@Mike,
Yes, that's what I read in the datasheet, so I stay with my first idea of 3.3v and 8MHz with 1 lipo.
What do you think about oxs at 8MHz Mike, could you help us ?

Re: OpenXSensor @8MHz

Posted: Tue Jul 01, 2014 9:26 pm
by jhsa
couldn't use the allegro current sensors anymore.. they say that they do work at 3.3V, but then they aren't precise anymore.. tested.. :)
What would be the advantage anyway?

João

Re: OpenXSensor @8MHz

Posted: Tue Jul 01, 2014 9:45 pm
by davx
The allegro ACS712 datasheet says 4.5v minimum, and current sensors that work with 3.3v do exist (ACS716...).

There is no particular advantage, lower consumption but it's not critical, just the possibility to use only 1 lipo cell.

I'm planning to put an oxs in a mini DLG powered with...1 lipo cell ;)

Re: OpenXSensor @8MHz

Posted: Tue Jul 01, 2014 9:50 pm
by jhsa
Ah. Ok. I do need sensors that measure more than 50A and unidirectional for better resolution.. for a dlg I presume ypu don't need a current sensor nor do you want the extra weight ;)

João

Re: OpenXSensor @8MHz

Posted: Tue Jul 01, 2014 10:12 pm
by mstrens
I have put all modifications for 8 mhz in the file in attachment.
The other files are unchanged.
Davx, can you try it on an arduino at 8 mhz?

Let me know if it works or not.

If it is ok, then I can put the code on google

Re: OpenXSensor @8MHz

Posted: Wed Jul 02, 2014 10:38 am
by davx
Hi,
Thanks mstrens, I can't try right away but as soon as can, I let you know if it works ;)

Re: OpenXSensor @8MHz

Posted: Mon Jul 07, 2014 4:57 pm
by davx
Hi,

I've mounted an OXS with an arduino pro mini 3.3v 8MHz and a MS5611 GY-63 board.
It seems to work as it should on the workbench, excellent !!

Now, I'm playing with a MCP9700A temperature sensor and I'd like to integrate it in OXS, I've come up with this code, which I've tested on an arduino UNO:

Code: Select all

/*
  Read MCP9700A temperature sensor with compensation and averaging
*/

#define VOLTREF 4.7
#define OFFSET 0
#define TSPIN A5

unsigned long lastTempMillis = 0;
int counter = 0;
float allTemp = 0.0;

float readMCP9700()
{
  int adc = analogRead(TSPIN);

  float tSensor = ( ( adc * ( VOLTREF / 1024.0 ) ) - 0.5 + OFFSET ) * 100;                                 // Read temp sensor      
  float error = -244e-6 * ( 125.0 - tSensor ) * ( tSensor - -40.0 ) + 2e-12 * ( tSensor - -40.0 ) + 2.0;   // Error calculated from AN1001 note
  float temp = tSensor - error;

  return temp;
}

void setup() {
  
  Serial.begin(9600);
}

void loop() {

  if ( millis() - lastTempMillis  > 200 )      // read temp sensor every 200 msec
  {
    counter++;

    allTemp = allTemp + readMCP9700();
    lastTempMillis = millis();
  }

  if ( counter == 5 )                          // Display average temp every 1 sec (5 samples)
  {
    Serial.print("  ");
    Serial.println( allTemp / 5.0 );

    counter = 0;
    allTemp = 0.0;
  }

}
Thanks.

Re: OpenXSensor @8MHz

Posted: Mon Jul 07, 2014 9:25 pm
by mstrens
davx wrote:Hi,

Now, I'm playing with a MCP9700A temperature sensor and I'd like to integrate it in OXS, I've come up with this code, which I've tested on an arduino UNO:
I had a very short look at MCP9700A datasheet.
It seems that it provides an analog voltage based on the temperature.

If it is really so, there is no need to change OXS code because OXS can already:
- read one (or several) analog voltage(s).
- calculate an averaging of several ADC measurements
- convert the average ADC value into another logical data applying an offset and a multiplier (this is part of the calibration of the voltage).
- transmit the converted value in the telemetry field that you want (e.g. T1 or T2).

All this is to set up in the config file.
I propose that you read the documentation in the config file. If it is not clear enough, please let me know and I can try to explain further

Re: OpenXSensor @8MHz

Posted: Mon Jul 07, 2014 9:59 pm
by davx
Yes, I've already read oxs_config.h but if you look at the code I've posted, there is a compensation error calculation and I don't think I could use it with the current OXS code.

By the way, this temp sensor should have a 0.5°C precision so perhaps it could be used to compensate the drifting of the MS5611 one...

Re: OpenXSensor @8MHz

Posted: Tue Jul 08, 2014 8:23 am
by mstrens
davx wrote:Yes, I've already read oxs_config.h but if you look at the code I've posted, there is a compensation error calculation and I don't think I could use it with the current OXS code.

By the way, this temp sensor should have a 0.5°C precision so perhaps it could be used to compensate the drifting of the MS5611 one...
Davx,
It would be quite easy to add the compensation error calculation to OXS.
Conversion of voltages is performed in the code in xos_arduino.cpp file.
In a first step, Arduino read each analog pin (being set up) and sums ADC values.
At regular intervals (500 msec), it calculates the averages and apply the offset/rate by the instruction:
arduinoData.mVolt[cntVolt] = (arduinoData.sumVoltage[cntVolt] / cnt * arduinoData.mVoltPerStep[cntVolt] ) + arduinoData.offset[cntVolt];

You can easily add the compensation there. If you plan to measure several voltages, you should add a test on "cntVolt" in order to perform the compensation error only for one specific voltage.

Still I am not sure that this compensation is really required for your use. I think it improves accuracy only when you are operating on a wide range of temperature. I expect that if if temperature is not stable, they would anyway be some significant differences between the real temperature of one object and the temperature of the sensor it self (due to the thermical inertie).

Please note that I think it is not usefull to use the temperature of this sensor to compensate the drift of MS5611.
Drift of MS5611 is (most probably) caused by the temperature drift of the MS5611 it self (and this one is probably not 100% correctly compensated).
MS5611 is in theory already compensated in temperature. It can measure his internal temperature whith a quite high precision and this internal temperature is used in OXS to calculate the pressure (and so the altitude).
I noticed that the internal MS5611 temperature can be much higher (e.g. 8 °c) than the temperature of the air around it. This results from the high frequency of the reading.
I noticed that the best way to reduce noise on the pressure (in order to get reliable vertical speed), I had to read the internal MS5611 temperature in high precision mode and with the same frequency as the pressure readings.

Re: OpenXSensor @8MHz

Posted: Tue Jul 08, 2014 11:56 am
by davx
@mstrens,

I've already started to create .h and .cpp files for the temperature sensor based on the current (A) sensor implementation.
If I fail, I'll do what you've suggested ;)

I understand better the MS5611 drifting now and I'd like to compare myself the temperature from the MS5611 and my temp sensor, how do I get this value ?

Re: OpenXSensor @8MHz

Posted: Tue Jul 08, 2014 12:55 pm
by mstrens
davx wrote: I understand better the MS5611 drifting now and I'd like to compare myself the temperature from the MS5611 and my temp sensor, how do I get this value ?
Davx,

The temperature from the MS5611 is calculated by this instruction in oxs_ms5611.cpp
TEMP = (2000 + (((int64_t)dT * (int64_t)_calibrationData[6]) >> 23)) / (float)100;

The value TEMP (temperature) is currently not stored in OXS because it is immediatelly used to calculate a raw pressure.
If you want to get this value just to make some comparison you have 2 options:
- the easiest one is just to add an instruction to "print" this value in the serial USB interface. Note that in order to activate the USB serial interface, you have to uncomment the "//#define DEBUG" instruction in the section 9 from config.h file. In this case you can only make tests when OXS is connected to a PC
- another solution could be to write the TEMP value in e.g. the field "varioData.sensitivity" and to configure the telemetry parameters from config.h in order to send the vario sensitivity in a field like T1 or T2. You can then get the value in your SD card log. Take care to set the instruction "varioData.sensitivity = expoSmooth ;" in comment in order to avoid overwriting the TEMP value. There are some proper ways to do the job but they require more changes and are difficult to explain.

Re: OpenXSensor @8MHz

Posted: Tue Jul 08, 2014 5:22 pm
by davx
@ mstrens,

Ok, thanks.

Re: OpenXSensor @8MHz

Posted: Wed Jul 09, 2014 11:37 am
by davx
Hi,

I'm progressing with my temp sensor integration and while doing so, I wondered if the files oxs_arduino.h and oxs_arduino.cpp could not be renamed as oxs_voltage.x because that's all they are doing, measuring voltages ;)

Re: OpenXSensor @8MHz

Posted: Wed Jul 09, 2014 12:00 pm
by mstrens
davx wrote:Hi,

I'm progressing with my temp sensor integration and while doing so, I wondered if the files oxs_arduino.h and oxs_arduino.cpp could not be renamed as oxs_voltage.x because that's all they are doing, measuring voltages ;)
You are right.
Those were the name of the file when I started working on the project and I never renamed them.
I can do it when I have time.
Note that the class and class items should be renamed too.

Re: OpenXSensor @8MHz

Posted: Wed Jul 09, 2014 6:54 pm
by davx
I've written oxs_temp.h and .cpp files for the MCP9700A sensor, it seems to work fine :)
They still need to polished, then I will forward them to you.
Just one thing, I'd like to get 1 decimal after the point but it must be on the OpenTX side I guess ?

I retrieved the MS5611 temperature with varioData.temperature, I think I've done it correctly as it works !

mstrens wrote: Those were the name of the file when I started working on the project and I never renamed them.
I can do it when I have time.
Note that the class and class items should be renamed too.
I've renamed "arduino" with "voltage" (and all that class something...), no compilation problem but not tested with real voltage measurements...

I'll be doing further tests and will communicate my files ;)

Re: OpenXSensor @8MHz

Posted: Wed Jul 09, 2014 7:03 pm
by mstrens
davx wrote: Just one thing, I'd like to get 1 decimal after the point but it must be on the OpenTX side I guess ?
Yes, Frsky telemetry protocol does not transmit digital (only integer) so formatting has to done on Tx side.
On OXS you only can apply some multiplier/divider that in some cases can help.

Re: OpenXSensor @8MHz

Posted: Sat Jul 12, 2014 9:18 am
by davx
Hi,
@mstrens,

You'll find attached my modified version of OXS:
- MCP9700A temp sensor support added
- Some parameters added in oxs_config.h (arduino vcc, temp sensor related, debug related...)
- oxs_arduino renamed with oxs_voltage
- numerous little cosmetics mods

I hope you'll find these mods interesting enough to put them in the official repository ;)

Some concerns though:
I can't get the cells function to work, with the settings of the oxs_config.h that I've joined:
- In HUB mode, I've got just 1 cell out of 3 and with a wrong measurement
- in S.Port mode, no cell at all

I can confirm that the volt(x) measurements are working correctly.

Perhaps a problem with the cell part of the code ?

Re: OpenXSensor @8MHz

Posted: Sat Jul 12, 2014 10:05 am
by mstrens
@ davx,
Thanks.
I will have a look and I will try to merge most add on in the official repository.
Still I can't do it directly because I am currently working on a version that support 2 MS5611 (one for altitude and one for compensated vario based on a TEK) and one 4525DO sensor (in order to get the airspeed). I can't completely finish it because I do not have the sensors to test it.
I have worked too on a OXS wizard in visal basic in order to generate automatically the config file based mainly on options selected in check box and drop lists. So each modification in oxs_config.h file has to be done in this visual basic program too.

I had already a look at your config file and I do not directly see a mistake. So I do not understand why you should not get the cells to work.
I presume that you check on the telemetry screen showing different voltages or that you made a custom screen displaying Cell and Cells.
Did you test it with the "official" OXS firmware?
Some months ago I tested it and it was OK.
I remember that it was not OK from the first time but that I fixed it. Perhaps did I forget to publish the fix(?).

Re: OpenXSensor @8MHz

Posted: Sat Jul 12, 2014 10:22 am
by davx
mstrens wrote:@ davx,
I will have a look and I will try to merge most add on in the official repository.
Still I can't do it directly because I am currently working on a version that support 2 MS5611 (one for altitude and one for compensated vario based on a TEK) and one 4525DO sensor (in order to get the airspeed). I can't completely finish it because I do not have the sensors to test it.
No problem, take the time you need ;)
I have worked too on a OXS wizard in visal basic in order to generate automatically the config file based mainly on options selected in check box and drop lists. So each modification in oxs_config.h file has to be done in this visual basic program too.
I know you've done much work on that already, but what do you think of switching to processing, the main advantage being that it's multi-platform and I could try to help (at least with the GUI)...
I had already a look at your config file and I do not directly see a mistake. So I do not understand why you should not get the cells to work.
I presume that you check on the telemetry screen showing different voltages or that you made a custom screen displaying Cell and Cells.
Did you test it with the "official" OXS firmware?
Some months ago I tested it and it was OK.
I remember that it was not OK from the first time but that I fixed it. Perhaps did I forget to publish the fix(?).
Yes, I've made a telemetry screen for all these values but if cells was working, the default screen should display the individual cells at the right, that's the case with HUB mode (but wrong values) but not with S.Port...
No, I didn't test with original OXS but i will do and comment later.

David

Re: OpenXSensor @8MHz

Posted: Sat Jul 12, 2014 11:37 am
by mstrens
davx wrote: I know you've done much work on that already, but what do you think of switching to processing, the main advantage being that it's multi-platform and I could try to help (at least with the GUI)...
I did not know about this tool.
I just had a very quick look.
My first meaning is that it not it not the easiest tool just to let the user select in check box and drop list.

Did you had a look at my beta version with visual basic available at :
viewtopic.php?f=86&t=4433&start=390.
Note: best to take the second version.

If you want to build a wizard based on "processing" and if you think that it would be better than what I did, it is not an issue at all for me. We could then publish your version instead of mine on google.

Re: OpenXSensor @8MHz

Posted: Sun Jul 13, 2014 4:59 pm
by davx
Hi,

I've made some tests with the original OXS code and cells function doesn't work either...
- In HUB mode, I can see the 3 cells display in the default telemetry screen but the values are wrong.
- In S.Port mode, no cells display on the default screen.
mstrens wrote: My first meaning is that it not it not the easiest tool just to let the user select in check box and drop list.
Processing is the original environment on which the arduino IDE is based and there is libraries dedicated to GUI.
Did you had a look at my beta version with visual basic available at :
Yes but it was a little buggy with the display...
If you want to build a wizard based on "processing" and if you think that it would be better than what I did, it is not an issue at all for me. We could then publish your version instead of mine on google.
I'll try to do something but I will certainly need help...

Bye

Re: OpenXSensor @8MHz

Posted: Sun Jul 13, 2014 5:14 pm
by jhsa
Thanks guys, that would be wonderful.

João