Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Motivation

There are some challenges building up the code structure that will run on the Arduino:

  1. Create an exchangeable interface that offers all methods, which are required to communicate with the hardware and environment.
    The benefit from this is, that the interface can be chosen depending on the sensors and if the flight is real or simulated.

  2. Make sure that within a single tick some sensor value is not measured multiple times just because it is requested on several occasions.
    This is important because every measurement takes time and consumes power. For that reason the above-mentioned interface should serve a resetTick function to know when a new tick hast begun. 

  3. Perform the implementation of the SCCharts-generated code as simple as possible.
    The Arduino code provides a setup method and a loop method, which are equivalent to the reset function and the tick function in SCCharts. Though KIELER can easily create some Arduino code out of any SCChart, important host code is missing:
    • At the very beginning of the Arduino code some dependencies like libraries and the interface need to be included.
    • Before every new tick the interface must be notified.


Basic Arduino sketch

QuadcopterController

Code Block
languagecpp
#include "CompInterface.h"
#include "FlightController.h"
CompInterface comp;

void setup() {
  reset();
}

void loop() {
  comp.resetTick();
  tick();
}

This file is not generated by KIELER. It includes the interface header and the header of the SSCharts-generated FlightController, which will be output in c, not explicit for Arduino.

In the setup and loop methods the reset and tick functions of the FlightController are triggered, while before the tick function the instanced CompInterface is notified about a new tick.

CompInterface.h

Code Block
languagecpp
#ifndef CompInterface_h
#define CompInterface_h
#include "Arduino.h"
#include "HardwareConnector.h"
#include "SimulationConnector.h"
class CompInterface {
  private:
  boolean simulation;
  
  public:
  CompInterface();
  void resetTick();
  void simulate(boolean active);
};
#endif

The CompInterface represents the communication interface to the sensors and actuators. It has a boolean, which knows if a simulation or real flight is running and gives the ability to get notified about a new tick.

Since the Arduino supports C++, most custom routines are written in it. This gives the ability to use classes for example. Note also that Arduino.h is an Arduino library for using Arduino methods in C++ code.

CompInterface.cpp

Code Block
languagecpp
#include "CompInterface.h"
boolean simulation;
HardwareConnector connector;
CompInterface::CompInterface() {
  simulation = false;
}
void CompInterface::resetTick() { 
  //TODO: Implement some code 
}
void CompInterface::simulate(boolean active) {
  simulation = active;
}

This file is just the implementation of the CompInterface.h.

Connector.h

Code Block
languagecpp
#ifndef Connector_h
#define Connector_h
class Connector { 
  public:
  virtual int getProximity() = 0;
  //TODO: Implement more methods
};
#endif

Connector.h creates an abstract class which cannot be instanced but forces all inheriting classes to implement the virtual methods equaling 0.

Heirs will be the HardwareConnector, which uses the real sensors and actuators for the methods, and the SimulationConnector, which contacts the simulation software for getting and setting values.

HardwareConnector.h / SimulationConnector.h

Code Block
languagecpp
#ifndef HardwareConnector_h
#define HardwareConnector_h
#include "Arduino.h"
#include "Connector.h"
class HardwareConnector : public Connector { 
  public:
  HardwareConnector();
  int getProximity();
};
#endif
Code Block
languagecpp
#ifndef SimulationConnector_h
#define SimulationConnector_h
#include "Arduino.h"
#include "Connector.h"
class SimulationConnector : public Connector { 
  public:
  SimulationConnector();
  int getProximity();
};
#endif

ConnectorThe HardwareConnector.h and SimulationConnector.h creates an abstract class which cannot be instanced but forces all inheriting classes to implement the virtual methods equaling 0.Heirs will be the HardwareConnector, which uses the real sensors and actuators for the methods, and the SimulationConnector, which contacts the simulation software for getting and setting valuesinherit from the Connector abstract class and thus implement the all of its virtual methods.

HardwareConnector.cpp / SimulationConnector.cpp

Code Block
languagecpp
#include "HardwareConnector.h"
HardwareConnector::HardwareConnector() {
}
int HardwareConnector::getProximity() {
  return 3;
}
Code Block
languagecpp
#include "SimulationConnector.h"
SimulationConnector::SimulationConnector() {
}
int SimulationConnector::getProximity() {
  return 4;
}

These are the pure implementations of the HardwareConnector and the SimulationConnector.