Building Arduino Projects
This project requires a lot of software and writing one large file is not practical. I started splitting functionality into multiple ".ino" files but even this became rather cumbersome. I wrote a custom c++ library to integrate to the remote-control mechanism as it was suitable for reuse in other projects. However, the rest of the code was less generic but I still wanted to split out functionality into subdirectries, similar to java packages so it can be tested in isolation.
The Arduino IDE does not directly allow splitting into subdirectories unless they are under a src directory. This is a reserved directory name. I split all the pin number definitions into a header file and setup the directory structure like this:
Note however: You cannot use relative paths to other modules such as #include ../../src/anotherModule.h so this technique is best for stand-alone functionality otherwise the directories need merging or you have to create further src subdirectories. Often it is easier to include this dependant code in an ino file in the main directory. This encourages all the code in the src/ subdirectories to have isolated functionality.
Directory Structure
Note the requirement to have a special src directory
test_moana/pin_definitions.h
test_moana/src
test_moana/src/sonarMaxbotix
test_moana/src/sonarMaxbotix/sonarMaxbotix.ino
test_moana/src/sonarMaxbotix/sonarMaxbotix.cpp
test_moana/src/sonarMaxbotix/sonarMaxbotix.h
|
The files that are in the src/sonarMaxbotix directory use these imports:
sonarMaxbotix.h
Note we need to import Arduino.h as some of the data types used in arduino are not standard c types
#ifndef SONARMAXBOTIX
#define SONARMAXBOTIX
#include < Arduino.h >
/**
* All maxbotix sonars configured to measure Pulse Width.
* When maxbotix RX pin is HIGH (or NC), then pulse width measured on PW pin. (free running).
* All sonars are disabled by default
*/
void assign_left_sonar_pins(const byte enablePin, const byte pwPin);
void assign_right_sonar_pins(const byte enablePin, const byte pwPin);
void assign_centre_sonar_pins(const byte enablePin, const byte pwPin);
void enablePingCentre(boolean trueEnables);
void enablePingLeft(boolean trueEnables);
void enablePingRight(boolean trueEnables);
long readPingCentre();
long readPingLeft();
long readPingRight();
// more generic
long readMaxbotixDurationPW(byte pwPin);
void switchMaxbotixSensor(byte triggerPin, boolean enable); // enable=true turns on the sensor
long readAverageMaxbotixDurationPW(byte pwPin, byte howMany);
#endif
|
sonarMaxbotix.cpp
Note the relative path to the pin_definitions.h file.
#include "sonarMaxbotix.h"
// Maxbotix sonars
const uint8_t PINGEN_LEFT = 23; // sonar left RX (enable)
const uint8_t PINGPW_LEFT = 25; // sonar left PW (echo)
. . .
assign_left_sonar_pins(PINGEN_LEFT, PINGPW_LEFT);
. . .
|
sonarMaxbotix.ino
Note the full relative path to sonarMaxbotix.h
#include "src/sonarMaxbotix/sonarMaxbotix.h"
// Maxbotix sonars
const uint8_t PINGEN_LEFT = 23; // sonar left RX (enable)
const uint8_t PINGPW_LEFT = 25; // sonar left PW (echo)
void setup() {
Serial.begin(57600);
Serial.println("Initialized maxbotix sonars");
sonar_setup();
}
void loop() {
sensor_test();
}
void sonar_setup() {
assign_left_sonar_pins(PINGEN_LEFT, PINGPW_LEFT);
. . .
}
void printSonar(char whichSonar[], long measured) {
Serial.print(whichSonar);
Serial.println(measured);
}
void sensor_test() {
delay(2000);
Serial.println("------------");
enablePingCentre(HIGH);
printSonar("Centre: ", readPingCentre());
enablePingCentre(LOW);
. . .
|
June 2023
|