HK Multiwii Pro with 3DR clone 90A current sensor

Post Reply
Batperson
Posts: 18
Joined: Mon May 11, 2015 2:52 pm

HK Multiwii Pro with 3DR clone 90A current sensor

Post by Batperson »

Hi,

I am attempting to use a clone APM power module's current sensor with the HK Multiwii Pro board and not having much success.

Since the A2 pin is not exposed I have put this in my config.h:

#define OVERRIDE_PSENSORPIN A14

And I have added this to line 442 of Multiwii.cpp (2.4) so that I can see the raw analogRead() value which should be coming from pin A14:

debug[0] = p;

What I am seeing is a value of 774 in the Debug 1 output in MultiWiiConfig when the flight controller is disarmed, and it only increases a few units if I throttle up until the copter is drawing around 6 amps. I measured the voltage from the current sensor, it is around 34mV with motors off when it draws around .7A from the battery. If I am correct, 34mV should give an analogRead() value of around 7, so it appears the flight controller is not reading the voltage from the current sensor correctly. Can anyone help me track down the problem?

I do have a voltage divider connected to pin A15 for battery voltage, and am getting sensible readings from that [edit] although it does seem to vary in accuracy by around 1V as the battery discharges. At least it is in the correct ballpark. [/edit]

Any help will be much appreciated.
Last edited by Batperson on Mon May 11, 2015 11:30 pm, edited 1 time in total.

QuadBow
Posts: 532
Joined: Fri Jan 04, 2013 10:06 am

Re: HK Multiwii Pro with 3DR clone 90A current sensor

Post by QuadBow »

Have you changed the value of PSENSORNULL to the value shown when disarmed? I guess it should be zero.
Have you changed PINT2mA to the appropriate value? It could be 90A=5V=1023 => PINT2mA = 90A/1023 =89mA per step.

Batperson
Posts: 18
Joined: Mon May 11, 2015 2:52 pm

Re: HK Multiwii Pro with 3DR clone 90A current sensor

Post by Batperson »

Yes, I experimented with PINT2mA and PSENSORNULL. I think I could find the correct values, but at the moment analogRead() value on A14 isn't what it should be according to the voltage going to A14, and doesn't vary as much as it should when the current flow varies. I wonder if there is something interfering with the pin, could it be not set to the correct mode or something like that?

I saw this in the file RX.cpp, could this have something to do with it as I am using pin A14? Unfortunately I'm not very knowledgeable about Arduino so I'm groping in the dark here.

void configureReceiver() {
/****************** Configure each rc pin for PCINT ***************************/
#if defined(STANDARD_RX)
#if defined(MEGA)
DDRK = 0; // !!!!!! defined PORTK as a digital port ([A8-A15] are consired as digital PINs and not analogical)
#endif
....


Another issue I ran into was that after flashing the board, changing the values for PSENSORNULL and PINT2mA and reflashing has no effect. This turned out to be because Multiwii is storing the values in EEPROM and using those values instead of the source code. The same thing occurs for VBATSCALE, but you can change that through the Gui. AFAIK there is no way to update the EEPROM values for the powermeter, so I added this code to EEPROM.cpp at line 55:

#if defined(POWERMETER)
pAlarm = (uint32_t) conf.powerTrigger1 * (uint32_t) PLEVELSCALE * (uint32_t) PLEVELDIV; // need to cast before multiplying
conf.pint2ma = PINT2mA; // until we have a way to set this through the GUI, ensure we always use config.h defines
conf.psensornull = PSENSORNULL;
#endif

QuadBow
Posts: 532
Joined: Fri Jan 04, 2013 10:06 am

Re: HK Multiwii Pro with 3DR clone 90A current sensor

Post by QuadBow »

Batperson wrote:DDRK = 0; // !!!!!! defined PORTK as a digital port ([A8-A15] are consired as digital PINs and not analogical)
That seems to be the explanation for your problems. A8..A15 are used for RX inputs. Can't you use A0..A7?

Batperson wrote:AFAIK there is no way to update the EEPROM values for the powermeter, so I added this code to EEPROM.cpp at line 55:

That's already done in the function update_constants():

Code: Select all

  #ifdef POWERMETER
    conf.pint2ma = PINT2mA;
  #endif
  #ifdef POWERMETER_HARD
    conf.psensornull = PSENSORNULL;
  #endif

and in function readEEPROM():

Code: Select all

  #if defined(POWERMETER)
    pAlarm = (uint32_t) conf.powerTrigger1 * (uint32_t) PLEVELSCALE * (uint32_t) PLEVELDIV; // need to cast before multiplying
  #endif

Batperson
Posts: 18
Joined: Mon May 11, 2015 2:52 pm

Re: HK Multiwii Pro with 3DR clone 90A current sensor

Post by Batperson »

That seems to be the explanation for your problems. A8..A15 are used for RX inputs. Can't you use A0..A7?


Not without directly soldering to the chip, which I don't think my soldering skills are up to. The HK Multiwii Pro board doesn't expose pins A0 - A7 which is frustrating. Do you know if there is a way to leave pins A14 and A15 as analog inputs?

That's already done in the function update_constants():


I did see that code, but it doesn't appear to be being called. Anyway after I edited EEPROM.cpp my changes to PINT2mA and PSENSORNULL took effect.

QuadBow
Posts: 532
Joined: Fri Jan 04, 2013 10:06 am

Re: HK Multiwii Pro with 3DR clone 90A current sensor

Post by QuadBow »

Batperson wrote:I did see that code, but it doesn't appear to be being called. Anyway after I edited EEPROM.cpp my changes to PINT2mA and PSENSORNULL took effect.
I will take effect if you clear the EEPROM via the tool multiwiiconf. (OK, it's an confusing implementation)

Batperson wrote:The HK Multiwii Pro board doesn't expose pins A0 - A7 which is frustrating.
So, my conclusion is that your board is not very compatible to the multiwii standard.

Batperson wrote:Do you know if there is a way to leave pins A14 and A15 as analog inputs?
You can change it, if you have a sum ppm or if you have at least 2 free pins free of the port A8..A15. But, in this case you leave the multiwii trunk. That emans you have to repeat this lesson at every new multiwii release...

Batperson
Posts: 18
Joined: Mon May 11, 2015 2:52 pm

Re: HK Multiwii Pro with 3DR clone 90A current sensor

Post by Batperson »

So, my conclusion is that your board is not very compatible to the multiwii standard.


Yes, I was disappointed to find that there are 8 analog pins I could possibly use for something that I don't have access to. But I do have pins A14 and A15 free as I am only using 6 channels from my radio.

You can change it, if you have a sum ppm or if you have at least 2 free pins free of the port A8..A15. But, in this case you leave the multiwii trunk. That emans you have to repeat this lesson at every new multiwii release...


I'm guessing it might be as simple as putting pinMode(PSENSORPIN, INPUT) in a suitable place in the initialization code, if this is happening because the pullup resistor is enabled? Which it seems may be the case as analogRead() on A14 returns 1023 if nothing is connected to the pin.

If so it might be worth putting it into the multiwii source, as it shouldn't (I imagine) affect people who are using the normal analog pins, and will mean that #define OVERRIDE_PSENSORPIN "just works" for people in my unfortunate situation :|

Batperson
Posts: 18
Joined: Mon May 11, 2015 2:52 pm

Re: HK Multiwii Pro with 3DR clone 90A current sensor

Post by Batperson »

Well, pinRead(PSENSORPIN, INPUT) does not do it. Nor does trying to disable the pullup resistor using digitalWrite(PSENSORPIN, HIGH).
If I comment out the setup code in RX.cpp like this:

Code: Select all

    /*
    #if defined(MEGA)
      DDRK = 0;  // defined PORTK as a digital port ([A8-A15] are consired as digital PINs and not analogical)
    #endif
    // PCINT activation
   
    for(uint8_t i = 0; i < PCINT_PIN_COUNT; i++){ // i think a for loop is ok for the init.
      PCINT_RX_PORT |= PCInt_RX_Pins[i];
      PCINT_RX_MASK |= PCInt_RX_Pins[i];
    }
    PCICR = PCIR_PORT_BIT;
    */


then analogRead(PSENSORPIN) returns 0, even though I am supplying 34.5mV to pin A14.

QuadBow
Posts: 532
Joined: Fri Jan 04, 2013 10:06 am

Re: HK Multiwii Pro with 3DR clone 90A current sensor

Post by QuadBow »

Code: Select all

    #if defined(MEGA)
      DDRK = 0;  // defined PORTK as a digital port ([A8-A15] are consired as digital PINs and not analogical)
    #endif
Actually, it looks good, but the description is wrong. DDRK = 0 means input - regardless whether digital input or analog input.

Try to change the following code in the MEGA section of the file def.h:

Code: Select all

  #define PCINT_PIN_COUNT            6
  #define PCINT_RX_BITS              (1<<2),(1<<4),(1<<5),(1<<0),(1<<1),(1<<3)
  #define PCINT_RX_PORT              PORTK
in order to avoid interferences with interrupts. PORTK should be zero (set up via PCINT_RX_BITS) for disabled pull-ups.

By the way: I provide only proposals, since I don't have the same board. So, I do not take any responsibility for damages...

Batperson
Posts: 18
Joined: Mon May 11, 2015 2:52 pm

Re: HK Multiwii Pro with 3DR clone 90A current sensor

Post by Batperson »

Yep I will give that a try and report back.

If all else fails I'll write a sketch that does nothing but analogRead() off A14 and see if I can get sensible results. Just for confirmation, 33.4mV *should* be detected by the Arduino right? I understand analogRead() has a resolution of 4.9mV per unit so 33.4mV should give a reading of 6 or 7 if the reference voltage is 5V which it should be.

Anyway, thanks for your help so far. I'm determined to get this working.

Batperson
Posts: 18
Joined: Mon May 11, 2015 2:52 pm

Re: HK Multiwii Pro with 3DR clone 90A current sensor

Post by Batperson »

The changes to def.h had the same effect as when I commented out the interrupt code in RX.cpp. A value of 0 from analogRead(). This was constant when powering the flight controller from my battery, but fluctuated up to around 25 when powered from the USB. For some reason the USB seems to affect the values read from the analog pins.

I wrote the following sketch to read from my 2 sensor ports A14 and A15:

Code: Select all

void loop()
{
  delay(10);       
  int a14 = analogRead(A14);    // read the input pin
  int a15 = analogRead(A15);
  Serial.print(a14);             // debug value
  Serial.print(" ");
  Serial.println(a15);
}


And got results like this when plugged in via the USB:
0 318
0 319
8 320
0 319
3 317
7 319
0 319
0 318
6 320
0 321
3 320
1 317
7 316
0 320
5 319

I wasn't able to get it to work over my Bluetooth module so don't know if it would be different when disconnected from the USB.

Now I'm not sure what more to try. Maybe this type of current sensor doesn't work well with Multiwii, or maybe it just doesn't work at all sharing pins with the RX.

Post Reply