由于arduino上没有调试实用程序(或者至少我不熟悉),因此很难按照代码查看它停止的位置... 下面我添加了我的设置功能。在内部设置中我创建了一个init类的对象..它的构造函数负责初始化任务时间等的EEPROM数据。但是通过添加几个串行输出,我发现程序不会脱离init类。当我评论init时,一切正常。我检查了所有init类的行,但没有发现问题
void setup() {
Serial.begin(9600);
pinMode(ARM_BUTTON_PIN,INPUT);
Serial.println("start");
// EEPROM.put(PACKET_COUNT_EEPROM, 0);
Init init;
Serial.println("before Contoll");
controller= new Controller();
Serial.println("setup");
delay(200);}
这是init.cpp:
#include "constants.h"
#include "Sampler.h"
#include "SDLogger.h"
#include "Controller.h"
#include "Init.h"
/*
* EEPROM Structure
* 0: @: 0 mission Time(s) uint16_t
* 1: @: sizeof(uint16_t) packetCount uint16_t
* 2: @: sizeof(uint16_t)+ sizeof(uint16_t) stateCode uint8_t
* 3: @: sizeof(uint16_t)+ sizeof(uint16_t) + sizeof(uint8_t) refAltitude int16_t
#define MISSION_TIME_EEPROM 0
#define PACKET_COUNT_EEPROM sizeof(uint16_t)
#define STATE_CODE_EEPROM sizeof(uint16_t)+ sizeof(uint16_t)
#define REF_ALTITUDE_EEPROM sizeof(uint16_t)+ sizeof(uint16_t) + sizeof(uint8_t)
*/
Init::Init(){
Sampler sampler;
uint16_t missionEEPROMTime ;
EEPROM.get(MISSION_TIME_EEPROM, missionEEPROMTime);
Serial.println("init");
delay(100);
if(!digitalRead(ARM_BUTTON_PIN)){
if( missionEEPROMTime == 0 ){
//initiate reset
Serial.println("armed0");
resetSystem();
Serial.println("befin");
delay(100);
initializeSystem();
Serial.println("befres");
Controller::startTime = 0;
} else{
Serial.println("armednot0");
//recover
recoverSystem();
Controller::startTime = missionEEPROMTime;
}
}else{
Serial.println("not arm");
resetSystem();
Controller::startTime = 0;
EEPROM.put(REF_ALTITUDE_EEPROM, sampler.readAltitude());
// Serial.println(sampler.readAltitude());
// while(1);
}
}
Init::resetSystem(){
Sampler sampler;
for (int i = 0 ; i < EEPROM.length() ; i++) {
EEPROM.write(i, 0);
}
EEPROM.put(REF_ALTITUDE_EEPROM, sampler.readAltitude());
}
Init::initializeSystem(){
delay(100);
Serial.println("vbqdfewf");
Serial.println("initSample");
EEPROM.put(MISSION_TIME_EEPROM, 0);
EEPROM.put(PACKET_COUNT_EEPROM, 0);
EEPROM.put(STATE_CODE_EEPROM, 0);
// EEPROM.put(REF_ALTITUDE_EEPROM, sampler.readAltitude());
// SDLogger::initCSV(0);
// SDLogger::initIMG(0);
// SDLogger::beginBus();
Serial.println("inited");
}
Init::recoverSystem(){
// SDLogger::initCSV(1);
// SDLogger::initIMG(1);
// SDLogger::beginBus();
}
和init.h:
#ifndef Init_h
#define Init_h
#include "constants.h"
#include "Sampler.h"
#include "SDLogger.h"
#include "Controller.h"
/*
* EEPROM Structure
* 0: @: 0 mission Time(s) uint16_t
* 1: @: sizeof(uint16_t) packetCount uint16_t
* 2: @: sizeof(uint16_t)+ sizeof(uint16_t) stateCode uint8_t
* 3: @: sizeof(uint16_t)+ sizeof(uint16_t) + sizeof(uint8_t) refAltitude int16_t
#define MISSION_TIME_EEPROM 0
#define PACKET_COUNT_EEPROM sizeof(uint16_t)
#define STATE_CODE_EEPROM sizeof(uint16_t)+ sizeof(uint16_t)
#define REF_ALTITUDE_EEPROM sizeof(uint16_t)+ sizeof(uint16_t) + sizeof(uint8_t)
*/
class Init {
public:
Init();
private:
resetSystem();
initializeSystem();
recoverSystem();
};
#endif
当我执行init init时,其他类在采样器和公墓中工作正常。所以他们没有问题。
答案 0 :(得分:0)
这也称为Static Initialzation Order Fiasco。您必须分离对象的构造(在setup
之前发生的事情)及其初始过程(在setup
或更晚的版本中)。
例如,Serial
和许多其他类都有begin()
或init()
方法,可以从草图中明确调用。