readAnalogVoltage - does it work?

Firmware/software/electronics/mechanics
Post Reply
ECiocca
Beginner
Posts: 6
Joined: Wed May 30, 2018 9:51 pm

readAnalogVoltage - does it work?

Post by ECiocca »

Hello All,

I'm having difficulty using the analog input for the CrazyFlie 2.0. I have a small circuit built which is testing the output of a pin (HELLO_LED) based on the voltage input of another pin (READ_LED). I have tested with various voltages, but the output LED never seems to trigger. Does anyone have some working code for readAnalogVoltage, or can you spot what's wrong with my code? Thank you!

Code: Select all

#define DEBUG_MODULE "HelloDeck"

#include "debug.h"

#include "stm32fxxx.h"

#include "FreeRTOS.h"
#include "timers.h"
#include "deck.h"
#include "param.h"

#define HELLO_LED DECK_GPIO_RX1
#define READ_LED DECK_GPIO_IO1

#define LED_ON HIGH
#define LED_OFF LOW


#define VBAT_TEST_VOLTAGE_LOW      (3.0 / ((1.0 + 69.0 + 10.0) / 10.0) * 0.95) /* 0.35625 */
#define VBAT_TEST_VOLTAGE_HIGH     (3.0 / ((1.0 + 69.0 + 10.0) / 10.0) * 1.05) /* 0.39375 */


 float fVoltage = 0.0F;
static xTimerHandle timer;

static void tfTimer(xTimerHandle timer)
{
	digitalWrite(HELLO_LED, LED_OFF);

	fVoltage=analogReadVoltage(READ_LED);

	if (fVoltage>VBAT_TEST_VOLTAGE_LOW)
	{
		digitalWrite(HELLO_LED, LED_ON);
	}
}

static bool isInit = false;

static void helloInit()
{
	if (isInit) {
	    return;
	  }
	DEBUG_PRINT("Hello Crazyflie 2.0!\n");
	adcInit();

	pinMode(HELLO_LED, OUTPUT);
	//pinMode(READ_LED, INPUT);

	digitalWrite(HELLO_LED, LED_OFF);

	timer = xTimerCreate("tfTimer", M2T(100), pdTRUE, NULL, tfTimer);
	xTimerStart(timer, 100);

	isInit = true;

}

PARAM_GROUP_START(hello)
PARAM_ADD(PARAM_FLOAT, fVoltage, &fVoltage)
PARAM_GROUP_STOP(hello)


static const DeckDriver helloDriver = {
 .name = "myHello",
 .init = helloInit,
 .usedGpio= DECK_USING_IO_1|DECK_USING_IO_4|DECK_USING_RX1,
};



DECK_DRIVER(helloDriver);
tobias
Bitcraze
Posts: 2127
Joined: Mon Jan 28, 2013 7:17 pm
Location: Sweden

Re: readAnalogVoltage - does it work?

Post by tobias »

I can't directly spot any problem. Is the driver started? Do you get the printed output to the console?
ECiocca
Beginner
Posts: 6
Joined: Wed May 30, 2018 9:51 pm

Re: readAnalogVoltage - does it work?

Post by ECiocca »

Hello tobias,

The problem may be if there is an additional driver I have not started, or something missing from the build configuration?
The deck code is loading, I can see its output on the console. I modified this deck to use digital input on the same pins and in that case it works - the LED on HELLO_LED will light when I pass the right voltage into the READ_LED pin.
tobias
Bitcraze
Posts: 2127
Joined: Mon Jan 28, 2013 7:17 pm
Location: Sweden

Re: readAnalogVoltage - does it work?

Post by tobias »

That is an unexpected behavior. In the analogRead function the pin is setup in analog mode before analog conversion so shouldn't be necessary to put it in digital input...
ECiocca
Beginner
Posts: 6
Joined: Wed May 30, 2018 9:51 pm

Re: readAnalogVoltage - does it work?

Post by ECiocca »

Right, I saw that. I'm not setting it to digital mode in my analog tests. But as a sanity check (to test if the pin had a short or something?) I tried it in digital mode instead and it worked - so I know the pin is physically ok. So I set the code back to using analog and it gets nothing useful, usually what seems to be a floating value around 0.1V that changes every time I restart the drone.

Does anyone have sample code for a basic deck doing analog input?
rampudia
Beginner
Posts: 14
Joined: Fri Mar 26, 2021 4:21 pm

Re: readAnalogVoltage - does it work?

Post by rampudia »

3 years later... but in case it helps someone I made a very simple program that uses the APP Layer https://www.bitcraze.io/documentation/r ... app_layer/ to send an analog value to the client's console:

Code: Select all

#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include "stm32fxxx.h"

#include "app.h"

#include "FreeRTOS.h"
#include "task.h"

#include "debug.h"

#include "log.h"
#include "param.h"
#include "deck_constants.h"
#include "deck_analog.h"

#define DEBUG_MODULE "INTERNLOGPARAM"

void appMain()
{
  DEBUG_PRINT("This is the App layer example of the internal log param api...\n");
  float a_read = 0.0f;
  adcInit();

  while(1) {
    vTaskDelay(M2T(100));

    a_read = analogRead(DECK_GPIO_TX2);
    DEBUG_PRINT("Analog read is now: %f \n", (double)a_read);
    
  }
}

I think the problem above was trying using the GPIO pins instead of using the Analog input ports (PA2 to PA7) https://wiki.bitcraze.io/projects:crazy ... ards:index
Post Reply