Add new sensor - arduino code


#1

Hi everyone,

I am trying to add some sensors to my OpenROV and I have some problems with
the arduino coding. So far I am trying to add a very simple analog input read and
display it to the telemetry stream.

Practically, I tried to modify the code that is already available for
controlling the lights of the ROV. However, when I add the code, I lose all
the telemetry data. Like I mess somehow with the streaming of the data.

Any advice or help would be welcome. The code is very simple, however I
think I am doing some fundamental mistake, or missing something.
The ROV is version 2.6 and running image 2.5.1.

Many thanks
Thomas

** These are all the changes I did:

* In OpenROV.ino added:
    #if(HAS_STD_ADCTEST)
     #include "Adctest.h"
     Adctest adctest;
    #endif

* In AConfig.h added:
    #define HAS_STD_ADCTEST (1)

* In Device.h added:
   #define ADCTEST_CAPABLE 7

* Created a new file Adctest.cpp:

    #include "AConfig.h"
    #if(HAS_STD_ADCTEST)
    #include "Device.h"
    #include "Pin.h"
    #include "Adctest.h"
    #include "Settings.h"
    #include <Arduino.h>
    #include "Timer.h"
 
    Timer update_timer;
 
    int sensorPin = A15;    
    float sensorValue = 0;  
 
    void Adctest::device_setup(){
        Settings::capability_bitarray |= (1 << ADCTEST_CAPABLE);    //not sure about this
        update_timer.reset();
    }
 
    void Adctest::device_loop(Command command){
       if (update_timer.elapsed (100)){
            sensorValue = analogRead(sensorPin);
            Serial.print(F("ADC_TEST:"));
            Serial.print(sensorValue);
            Serial.println(';');
        }
    }
    #endif

 
* And also the header file for this:
    #ifndef __ADCTEST_H_
    #define __ADCTEST_H_
    #include <Arduino.h>
    #include "Device.h"
    #include "Pin.h"
    #include "AConfig.h"
 
    #if(HAS_STD_CAPE)
     #include "Cape.h"
    #endif
 
    #if(HAS_OROV_CONTROLLERBOARD_25)
      #include "controllerboard25.h"
    #endif
 
    class Adctest : public Device {
      public:
        Adctest():Device(){};
        void device_setup();
        void device_loop(Command cmd);
    };
    #endif

#2

Glad to see you’re figuring it out!

The ADCTEST_CAPABLE is not hurting anything, but the capabilities array is intended to signal to the rest of the system the features that are enabled on the ROV firmware. In this way we can have UI elements automatically enable or disable themselves as needed.

My initial guess is that you are hitting a limit in the number of devices that have been added. There is a fixed array and the system does not give a good error message when the number of devices is exceeded. I keep having to move that number up as I add new devices.


#3

Thanks Brian, indeed this was the problem. Works now.

Is there any recommended way to develop (and debug) on the Arduino, without having to go through the Beaglebone and the cockpit?


#4

That’s an area that can be better explored. Using another Arduino mega board + visual micro + atmel ice hardware debugger will let you step through the lines of code and set breakpoints.

You can get an SPI programmer and use the controller board sans the beaglebone so that you can use the Arduino IDE directly… essentially treating the controllerboard like a standard Arduino board.

I personally keep the logic simple and use plenty of print statements. For the complex logic I write unit tests that validate the logic for me. You can see the unit tests in the test folder.