Adafruit is open and shipping! Let's build back better, together!
0

Problem using libraries servo+radiohead on feather 32u4 RFM9
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Problem using libraries servo+radiohead on feather 32u4 RFM9

by antoinepigne on Fri Jun 16, 2017 5:36 am

Hello,
I'm posting this topic because I'm trying to use servos and the radio at the same time on a feather 32u4 RFM95W LoRa radio 433Mhz.
But when I want to compile it seams that there is a conflict between the library Servos (from Arduino) and the library RadioHead.
The code is the following :

Code: Select all | TOGGLE FULL SIZE
#include <Servo.h>

#include <RHReliableDatagram.h>
#include <RH_RF95.h>
#include <SPI.h>

#define REMOTE_ADDRESS 1
#define BLIMP_ADDRESS 2

// Singleton instance of the radio driver
#define RFM95_CS 8
#define RFM95_RST 4
#define RFM95_INT 7

typedef enum {
  PING = 0,
  BLIMP_MOTOR_CMD = 1,
  BASE_MOTOR_CMD = 2,
  BATT_STATUS = 3,
  ERROR_MSG = 4,
  MSG = 5
} motor_t;

// Singleton instance of the radio driver
RH_RF95 rf95(RFM95_CS, RFM95_INT);

//RHReliableDatagram manager(driver, BLIMP_ADDRESS);

#define NB_SERVOS 4
Servo servos[NB_SERVOS];
int servo_pins[NB_SERVOS] = {10, 11, 12, 13};

void setup()
{
  Serial.begin(9600);
  while (!Serial)
    ; // Wait for serial port to be available
  rf95.init();
  Serial.println("[lora] ready");

  // servo
  for (size_t i = 0; i < NB_SERVOS; ++i)
  {
    pinMode(servo_pins[i], OUTPUT);
    servos[i].attach(servo_pins[i]);
    servos[i].write(90);
  }

  // led
  pinMode(13, OUTPUT);

  // battery
  pinMode(9, INPUT);

  // power
  rf95.setTxPower(23, false);
}

// Dont put this on the stack:
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN] = {0};

void ping()
{
  Serial.println("Received a PING");
  uint8_t ping_packet[] = {PING};
  rf95.send(ping_packet, sizeof(ping_packet));
}

//
void motor_cmd(const uint8_t *str, int len)
{
  Serial.print("Received a MOTOR_CMD:");
  Serial.println((int)str[1]);
  // str[0] is the command (MOTOR_CMD), then we have the positions
  for (int i = 1; i < len; ++i)
  {
    int sp = (int)str[i];
    servos[i - 1].write(sp);
  }
}

void batt_status()
{
  float val = analogRead(A7) * 2 * 3.3 / 1024;
  Serial.print("Received BATT_STATUS:");
  Serial.println(val);
  uint8_t data[2] = {BATT_STATUS, uint8_t(val * 10)};
  rf95.send((uint8_t *)data, 2);
}

void handle_response(const uint8_t *str, int len)
{

  // first byte is the instruction
  switch (str[0])
  {
    case PING:
      ping();
      break;
    case BLIMP_MOTOR_CMD:
      motor_cmd(str, len);
      break;
    case BATT_STATUS:
      batt_status();
      break;
    default:
      Serial.print("ERROR: unrecognized instruction type:");
      Serial.println((int)str[0]);
  }
}

void loop()
{
  if (rf95.waitAvailableTimeout(100))
  {
    digitalWrite(13, HIGH);
    // Wait for a message addressed to us from the client
    uint8_t len = sizeof(buf);
    uint8_t from;
    if (rf95.recv(buf, &len))
      handle_response(buf, len);
    digitalWrite(13, LOW);
  }
}



And the error is :
libraries\RadioHead\RH_ASK.cpp.o (symbol from plugin): In function `RH_ASK::maxMessageLength()':

(.text+0x0): multiple definition of `__vector_17'

libraries\Servo\avr\Servo.cpp.o (symbol from plugin):(.text+0x0): first defined here

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Adafruit Feather 32u4.


If anyone have an answer, I would appreciate :)

Thank you.

antoinepigne
 
Posts: 3
Joined: Fri Mar 31, 2017 10:16 am

Re: Problem using libraries servo+radiohead on feather 32u4

by adafruit_support_rick on Fri Jun 16, 2017 9:55 am

Both the Servo library and the RadioHead library use Timer 1, so that's what the conflict is. You can apparently modify the RadioHead library to use Timer2 instead.
Edit the library file RH_ASK.cpp. You should see the following at line 22. Uncomment the #define for RH_ASK_ARDUINO_USE_TIMER2
Code: Select all | TOGGLE FULL SIZE
// RH_ASK on Arduino uses Timer 1 to generate interrupts 8 times per bit interval
// Define RH_ASK_ARDUINO_USE_TIMER2 if you want to use Timer 2 instead of Timer 1 on Arduino
// You may need this to work around other librraies that insist on using timer 1
// Should be moved to header file
//#define RH_ASK_ARDUINO_USE_TIMER2

adafruit_support_rick
 
Posts: 35095
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: Problem using libraries servo+radiohead on feather 32u4

by antoinepigne on Fri Jun 16, 2017 10:47 am

I've already done that and when I compile I have another error :
C:\Program Files (x86)\Arduino\libraries\RadioHead\RH_ASK.cpp: In member function 'void RH_ASK::timerSetup()':

C:\Program Files (x86)\Arduino\libraries\RadioHead\RH_ASK.cpp:291:5: error: 'TCCR2A' was not declared in this scope

TCCR2A = _BV(WGM21); // Turn on CTC mode)

^

In file included from c:\users\antoi\appdata\local\arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.4-arduino2\avr\include\avr\io.h:99:0,

from c:\users\antoi\appdata\local\arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.4-arduino2\avr\include\avr\pgmspace.h:90,

from C:\Users\antoi\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.19\cores\arduino/Arduino.h:28,

from C:\Program Files (x86)\Arduino\libraries\RadioHead/RadioHead.h:805,

from C:\Program Files (x86)\Arduino\libraries\RadioHead/RHGenericDriver.h:9,

from C:\Program Files (x86)\Arduino\libraries\RadioHead/RH_ASK.h:9,

from C:\Program Files (x86)\Arduino\libraries\RadioHead\RH_ASK.cpp:6:

C:\Program Files (x86)\Arduino\libraries\RadioHead\RH_ASK.cpp:291:18: error: 'WGM21' was not declared in this scope

TCCR2A = _BV(WGM21); // Turn on CTC mode)

^

C:\Program Files (x86)\Arduino\libraries\RadioHead\RH_ASK.cpp:293:5: error: 'TCCR2B' was not declared in this scope

TCCR2B = prescaler;

^

C:\Program Files (x86)\Arduino\libraries\RadioHead\RH_ASK.cpp:297:5: error: 'OCR2A' was not declared in this scope

OCR2A = nticks;

^

C:\Program Files (x86)\Arduino\libraries\RadioHead\RH_ASK.cpp:304:5: error: 'TIMSK' was not declared in this scope

TIMSK |= _BV(OCIE2A);

^

In file included from c:\users\antoi\appdata\local\arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.4-arduino2\avr\include\avr\io.h:99:0,

from c:\users\antoi\appdata\local\arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.4-arduino2\avr\include\avr\pgmspace.h:90,

from C:\Users\antoi\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.19\cores\arduino/Arduino.h:28,

from C:\Program Files (x86)\Arduino\libraries\RadioHead/RadioHead.h:805,

from C:\Program Files (x86)\Arduino\libraries\RadioHead/RHGenericDriver.h:9,

from C:\Program Files (x86)\Arduino\libraries\RadioHead/RH_ASK.h:9,

from C:\Program Files (x86)\Arduino\libraries\RadioHead\RH_ASK.cpp:6:

C:\Program Files (x86)\Arduino\libraries\RadioHead\RH_ASK.cpp:304:18: error: 'OCIE2A' was not declared in this scope

TIMSK |= _BV(OCIE2A);

^

exit status 1
Error compiling for board Adafruit Feather 32u4.



And I really don't know where we should declare these variables, there isn't this problem when we use timer 1. So I guess that there is a location where they declare these variables for timer 1 but not for timer 2.
Or I was supposing that these variables are variables from the boards ?

Thank you.

antoinepigne
 
Posts: 3
Joined: Fri Mar 31, 2017 10:16 am

Re: Problem using libraries servo+radiohead on feather 32u4

by adafruit_support_rick on Fri Jun 16, 2017 2:51 pm

Aha! the 32U4 doesn't have a Timer2.
The library is looking for a 16-bit timer, which would be Timer3 on the 32U4. The library code will have to be modified to use Timer3 instead of Timer2.

Pretty much, you'll just be substituting a '3' for the '2' in those defines.
So, TCCR3A instead of TCCR2A, TCCR3B instead of TCCR2B, WGM31 instead of WGM21, etc.

adafruit_support_rick
 
Posts: 35095
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: Problem using libraries servo+radiohead on feather 32u4

by Wrobelekpl on Tue Feb 06, 2018 11:18 pm

I have the same problem but with RH_RF95. Im not sure how to change the timer on it.

Wrobelekpl
 
Posts: 4
Joined: Tue Feb 06, 2018 11:11 pm

Re: Problem using libraries servo+radiohead on feather 32u4

by outlandtech0 on Sat Jun 02, 2018 2:58 pm

Are there any libraries and Sketches that work out of the box for the feather 32u4 RFM9? instructions to change timer2 to timer3, etc doesn't help me

outlandtech0
 
Posts: 1
Joined: Thu Aug 09, 2012 8:16 pm

Please be positive and constructive with your questions and comments.