question about the Deck-driver structure

Firmware/software/electronics/mechanics
anasios
Beginner
Posts: 14
Joined: Fri Nov 06, 2015 3:41 pm

question about the Deck-driver structure

Post by anasios »

Hi,

I wanna connect 3 IR sensors to the CF and for this, we have to create a deck driver which contains the pins at which the sensors are connected. I started by setting up the first sensor as following :

I will solder it at the RX1 pin (the fist analog input) and i have create a function which reads the value of this sensor. However, i don't know what .vid , .pid and .name mean ! Could anyone explain me what that means ?
and if i don't want to test my sensor, Can i remove this line : .test= ......... ?

thank you in advance

Code: Select all

#include "deck.h"
#include "deck_core.h"

void mySensorInit(DeckInfo *info)
{
  pinMode(DECK_GPIO_RX1,INPUT);     // Set my sensor as an input
}

int SensorValue ()
{
	int value=0,
	value=digitalRead(DECK_GPIO_RX1);    // Reading the value of the sensor
}

static const DeckDriver mySensor_driver = {
  .vid = 0,
  .pid = 0,
  .name = "meMyled",


  .usedGpio = DECK_USING_RX1,

  .init = mySensorInit,
  .test = mySensorTest,
};

DECK_DRIVER(mySensor_driver);
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: question about the Deck-driver structure

Post by arnaud »

VID/PID are numbers we assign the boards. The aim is to provide an easy to check number for each board. For development just left them to 0.
This is documented there: https://wiki.bitcraze.io/doc:crazyflie: ... mat#header. If you do not want noise just remove .vid and .pid, they will be set at 0 by default.

.test is optional, you can remove it.

If your driver gets initialized you will see it in the console.
anasios
Beginner
Posts: 14
Joined: Fri Nov 06, 2015 3:41 pm

Re: question about the Deck-driver structure

Post by anasios »

thank you for the answers ! An other question ^^ Can we define 3 IR sensors in the same structure as follows ?

Code: Select all

#include "deck.h"
#include "deck_core.h"

void mySensorInit(DeckInfo *info)
{
  pinMode(DECK_GPIO_RX1,INPUT); // Set my sensor 1 as an input
  pinMode(DECK_GPIO_TX1,INPUT); // Set my sensor 2 as an input
  pinMode(DECK_GPIO_SCK,INPUT); // Set my sensor 3 as an input
}

float SensorValue ()
{
	float value=0;
	float value2=0;
	float value3=0;

	value=digitalRead(DECK_GPIO_RX1);// Reading the value of the sensor 1
	value2=digitalRead(DECK_GPIO_TX1); // Reading the value of the sensor 2
	value3=digitalRead(DECK_GPIO_SCK); // Reading the value of the sensor 3
}

static const DeckDriver mySensor_driver = {

  .usedGpio = DECK_USING_RX1,
  .usedGpio = DECK_USING_TX1,
  .usedGpio = DECK_USING_SCK,
  .init = mySensorInit,
};

DECK_DRIVER(mySensor_driver);
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: question about the Deck-driver structure

Post by arnaud »

Hi,

To define many pin used you should 'or' them instead, also the name is mandatory if you want to be able to enable the driver:

Code: Select all

static const DeckDriver mySensor_driver = {
  .name = "meIrSensors",

  .usedGpio = DECK_USING_RX1 | DECK_USING_TX1 |  DECK_USING_SCK,
  .init = mySensorInit,
};
The deck API is not very advanced yet and to run code you will have to use the FreeRTOS API. For example I modified your file to use a timer to poll the sensors 10 times per second and added the readings as log values to that you can graph them from the PC client:

Code: Select all

#include "deck.h"

#include "log.h"

#include "FreeRTOS.h"
#include "timers.h"

static xTimerHandle timer;
static float range1;
static float range2;
static float range3;


// This function will be called by the FreeRTOS timer every 100ms
void SensorValue(xTimerHandle timer)
{
   range1=analogRead(DECK_GPIO_RX1); // Reading the value of the sensor 1
   range2=analogRead(DECK_GPIO_TX1); // Reading the value of the sensor 2
   range3=analogRead(DECK_GPIO_SCK); // Reading the value of the sensor 3
}

void mySensorInit(DeckInfo *info)
{
  pinMode(DECK_GPIO_RX1,INPUT); // Set my sensor 1 as an input
  pinMode(DECK_GPIO_TX1,INPUT); // Set my sensor 2 as an input
  pinMode(DECK_GPIO_SCK,INPUT); // Set my sensor 3 as an input

  // Create and start the update timer with a period of 100ms
  timer = xTimerCreate("irsensorTmr", M2T(100), pdTRUE, NULL, SensorValue);
  xTimerStart(timer, 100);
}

static const DeckDriver mySensor_driver = {
  .name = "myIrSensors",

  .usedGpio = DECK_USING_RX1 | DECK_USING_TX1 |  DECK_USING_SCK,
  .init = mySensorInit,
};

DECK_DRIVER(mySensor_driver);

// Add sensor reading as log variables
LOG_GROUP_START(myIrSensors)
LOG_ADD(LOG_FLOAT, range1, &range1)
LOG_ADD(LOG_FLOAT, range2, &range2)
LOG_ADD(LOG_FLOAT, range3, &range3)
LOG_GROUP_STOP(myIrSensors)
I just tested that it compiled.

What IR sensors are you using? I feel this project could be a nice example/tutorial to add on the wiki.
anasios
Beginner
Posts: 14
Joined: Fri Nov 06, 2015 3:41 pm

Re: question about the Deck-driver structure

Post by anasios »

Thank you for your help! I have a plainty of questions about the code modifications. In fact, I'm using sensors with digital inputs (GP2Y0D810Z0F) that's why I have used digitalRead instead of analogRead in the code above.
Moreover, i have created a .h file in which i put all prototype functions as follows:

Code: Select all

#ifndef SENSOR_H_
#define SENSOR_H_
#include <stdbool.h>

void mySensorInit();
void SensorValue ();


#endif /* SENSOR_H_ */
It compiles successfully, but i can't flash my CF by radio. Note that the sensor.c(src) and .h(interface) are put in the same stabilizer.c folder
anasios
Beginner
Posts: 14
Joined: Fri Nov 06, 2015 3:41 pm

Re: question about the Deck-driver structure

Post by anasios »

In fact, i'm using an IR-sensor with a digital output and I wanna know weither we have to use DECK_GPIO_IO1 or just an analog input (DECK_GPIO_RX1).
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: question about the Deck-driver structure

Post by arnaud »

Hi,

If you use digital sensors then digitalRead will be enough. I got confused because you are using float so I assumed the sensor was analog. All the pins of the expansion port can be used as digital input so you can use any of them.

Your .h file is not required, your code is register with the "DECK_DRIVER(mySensor_driver);" line and the deck subsytem will call your init function.

To flash your new firmware refer to the wiki documentation: https://wiki.bitcraze.io/doc:crazyflie: ... re_upgrade
If you have the client cloned in the same folder as the firmware you can simply type "make cload" and start the Crazyflie in bootloader mode.

If you have successuly enabled your driver you will see it in the console, for example on my copter I enabled the dwm1000 driver:

Code: Select all

DECK_DRIVERS: Found 5 drivers
DECK_DRIVERS: VID:PID 0:0 (bcBuzzer)
DECK_DRIVERS: VID:PID BC:3 (bcBigQuad)
DECK_DRIVERS: VID:PID BC:FF (bcExpTest)
DECK_DRIVERS: VID:PID BC:1 (bcLedRing)
DECK_DRIVERS: VID:PID 0:0 (bcDWM1000)
DECK_CORE: 0 deck enumerated
DECK_CORE: Initializing compile-time forced driver 'bcDWM1000'
EEPROM: I2C connection [OK].
AK8963: Self test [OK].
LPS25H: Self test [OK].
DWM: Chip ID: DECA0130
DECK_CORE: Compile-time forced driver 'bcDWM1000' test [OK]
SYS: Free heap: 4120 bytes
The driver compiled are listed if you compile with DEBUG=1 in tools/make/config.mk.
anasios
Beginner
Posts: 14
Joined: Fri Nov 06, 2015 3:41 pm

Re: question about the Deck-driver structure

Post by anasios »

it seems very interesting what you've done! Could you please tell how to visualize the driver state ?
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: question about the Deck-driver structure

Post by arnaud »

In my console I see that the driver is found in the build with:

Code: Select all

DECK_DRIVERS: VID:PID 0:0 (bcDWM1000)
I know that its init function is called with:

Code: Select all

DECK_CORE: Initializing compile-time forced driver 'bcDWM1000'
And I know that my test function has been called and did succed with:

Code: Select all

DECK_CORE: Compile-time forced driver 'bcDWM1000' test [OK]
anasios
Beginner
Posts: 14
Joined: Fri Nov 06, 2015 3:41 pm

Re: question about the Deck-driver structure

Post by anasios »

When i compiled, i never found the driver-state. I don't know why! When we use a timer, have we to put .usedPeriph=DECK_USING_TIMER3 in my structure ?
Post Reply