Hi, I am making a quadx with Nano (CH340) board and multiwii firmware with modifications. I will have MPU6050 for gyro and acc, HC05 for bluetooth and HC-sr04 ultrasonic distance sensor. The idea is to use RemoteXY to control the quadx with the distance sensor to keep at a constant low altitude. Here, (https://www.instructables.com/Cheap-Arduino-Drone-Using-Bluetooth/) Jovian_Dsouza showed how bluetooth is used so that it can be controlled by RemoteXY. And here, Sonar HC-SR04 Support now implemented in MW2.4 - MultiWii, [jaysonragasa] showed how to integrate HC-SR04 sonar. For my purpose, I combined the two codes with modifications to the apparent places, e.g. Serial_SUM_PPM in config.h. The new codes do not compile in Arduino IDE, it complains about multiple definition of vector 3, I pinned it down to this code in sensors.cpp:
Then I changed the bluetooh.cpp file to use harewareserial (the rx and tx pins) instead of softwareserial(using pins 8 and 7), again, it complains about multiple definition of vector 18 and 19. It appears that the conflict code is in serial.cpp:
this function is used in multiwii.cpp in the setup() function and other files such as gps.cpp, telemetry.cpp, lcd.cpp.
In short, looks like if I use softwareserial for bluetooth, it conflicts with the HC-sr04 sonar, if i use hardwareserial, it conflicts with the main file multiwii.cpp. I wonder whether it is practically possible to put these components together in Nano. Your input is greatly appreciated.
The problem is that your conflict code in serial.cpp doesn't know about the Nano. It provides only support for the Pro Mini, the Pro Micro and for the Mega. You might have to add support for the Nano, which should be very similar to the Pro Mini version as they use the same processor.
I was wondering about this at first, but this is from the stock code. Multiwii allows Nano when we choose Nanowii in config.h. The compiling error points to multiple definition of vector_18 and 19.
Post the complete output! In that case the provided excerpt isn't responsible for the error.
The problem with SoftwareSerial will consist as it uses the Pin Change Interrupt as well as the sonar code. If you must use both you have to rewrite both libraries and merge the two interrupt handlers.
Thank you for your information. In the code all ATmega328p is defined as promini in def.h.
#define PROMINI
in multiwii.cpp in setup(), there is:
SerialOpen(0,SERIAL0_COM_SPEED);
So it appears once the multiwii code starts, it opens the hardwareserial.
Here is the output with hardwareserial:
HardwareSerial0.cpp.o (symbol from plugin): In function `Serial':
(.text+0x0): multiple definition of `__vector_18'
...\AppData\Local\Temp\arduino_build_310860\sketch\Serial.cpp.o (symbol from plugin):(.text+0x0): first defined here
HardwareSerial0.cpp.o (symbol from plugin): In function `Serial':
(.text+0x0): multiple definition of `__vector_19'
...\AppData\Local\Temp\arduino_build_310860\sketch\Serial.cpp.o (symbol from plugin):(.text+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
Using library RemoteXY at version 3.1.9 in folder: ...\Documents\Arduino\libraries\RemoteXY
exit status 1
Error compiling for board Arduino Nano.
Here is the output with softwareserial:
...\AppData\Local\Temp\arduino_build_310860\libraries\SoftwareSerial\SoftwareSerial.cpp.o (symbol from plugin): In function `SoftwareSerial::read()':
(.text+0x0): multiple definition of `__vector_3'
...\AppData\Local\Temp\arduino_build_310860\sketch\Sensors.cpp.o (symbol from plugin):(.text+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
Using library SoftwareSerial at version 1.0 in folder: ...\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\libraries\SoftwareSerial
Using library RemoteXY at version 3.1.9 in folder: ...\Documents\Arduino\libraries\RemoteXY
exit status 1
Error compiling for board Arduino Nano.
Sorry for this bulky post, I am unable to attach a file as a new user. Since the softwareserial seems to be problematic with ISR. I really hope to make hardwareserial work. Greatly appreciate your help!
If you use that library you must not use the Serial objects the core offers, otherwise you get the serial interrupts doubly defined.
As I already wrote, the SoftwareSerial and your sonar library both use the pin change interrupt and you can have only one handler for that.
You cannot assume that every Arduino library doesn't conflict with any other Arduino library. They usually can coexist if no hardware features are used directly (just be the base Arduino libraries) but in your setup this doesn't seem to be the case.
Sounds this is a simlar scenario to using softwareserial. I tried to comment out the serialopen() call in multiwii.cpp, the same error message during compiling. I also tried to comment out case 0 in serialopen() function (inside serial.cpp):
I got the samae error message.
If I cannot use the hardware serial this way in my code (core lib hardwareserial.h), can you point me to a link of example so I can figure out how to use the serial object created by the multiwii core code in my code to use bluetooth? Thanks a lot.
I played with the codes a bit more and was able to narrow it down to two lines in serials.cpp that caused the error when using hardwareserial. They are not the serialopen() function that I mentioned before. Here are the two lines:
#if defined(PROMINI)
ISR(USART_UDRE_vect) { // Serial 0 on a PROMINI
Now my question is: what should i look for in the code of multiwii if I'm going to find out what is using these hardserial interrupts? I'm thinking, if the things using these interrupts are not what I am using e.g. Serial_SUM_PPM receiver, I may simply comment these two lines out.
The tabs that contain the serial.h lib in the header are: GPS.ccp, LCD.ccp, multiwii.cpp, protocol.cpp, RX.cpp, telemetry.cpp.
You still haven't posted your complete code so we don't know what exactly you're doing. I guess there is a reason why that multiwii software has code for the hardware serial interface, probably they have a device connected there. You cannot connect additional hardware to a serial interface already used by another device. If there isn't any other device connected to the hardware serial, why does the project have low level code to use it?
That probably won't work.
GPS is a good candidate for a device that might be connected to the hardware serial interface. Do you know what hardware you have?
Thanks Pylon for your comments and help. Actually I had a more detailed look at the codes and tested the codes. I realized that the USB plug is using the serials because the code needs to connect to the GUI (multiwii configurator). Probably, altsoftserial would solve the problem when RomoteXY supports it (not yet). Or alternatively I can comment these lines out and upload for flight, in that case, I will be unable to connect it to the GUI. To connect it to GUI, I can disable bluetooth.
At least AltSoftSerial doesn't use the pin change interrupt so you don't get a conflict there. But keep in mind that AltSoftSerial doesn't allow to choose the pins freely they are fixed and only depend on the processor used.