From 86feb8bf7b03cfd6588a4bde2e6c3611e0820a74 Mon Sep 17 00:00:00 2001 From: "james.goppert" Date: Thu, 30 Dec 2010 05:25:28 +0000 Subject: [PATCH] Fixed issues with RcChannel EEPROM var ownership. git-svn-id: https://arducopter.googlecode.com/svn/trunk@1369 f9c3cf11-9bcb-44bc-f272-b75c42450872 --- libraries/AP_Common/AP_Var.h | 8 ++- libraries/AP_Common/c++.cpp | 6 +++ libraries/AP_EEProm/AP_EEProm.cpp | 14 +++++ libraries/AP_EEProm/AP_EEProm.h | 12 ++++- libraries/AP_RcChannel/AP_RcChannel.cpp | 27 +++++++++- libraries/AP_RcChannel/AP_RcChannel.h | 29 ++--------- .../examples/ServoSweep/ServoSweep.pde | 52 ++++++++++--------- 7 files changed, 92 insertions(+), 56 deletions(-) diff --git a/libraries/AP_Common/AP_Var.h b/libraries/AP_Common/AP_Var.h index 5154ebca4c..7e7c87ef32 100644 --- a/libraries/AP_Common/AP_Var.h +++ b/libraries/AP_Common/AP_Var.h @@ -52,8 +52,8 @@ class AP_Var : public AP_VarI { public: /// The default constrcutor - AP_Var(const type & data, const char * name = "", const bool & sync=false) : - _data(data), _name(name), _sync(sync) + AP_Var(const type & data, const char * name = "", const char * parentName = "", const bool & sync=false) : + _data(data), _name(name), _parentName(parentName), _sync(sync) { } @@ -118,6 +118,9 @@ public: /// Get the name. This is useful for ground stations. virtual const char * getName() { return _name; } + /// Get the parent name. This is also useful for ground stations. + virtual const char * getParentName() { return _parentName; } + /// If sync is true the a load will always occure before a get and a save will always /// occure before a set. virtual const bool & getSync() { return _sync; } @@ -126,6 +129,7 @@ public: protected: type _data; /// The data that is stored on the heap */ const char * _name; /// The variable name, useful for gcs and terminal output + const char * _parentName; /// The variable parent name, useful for gcs and terminal output bool _sync; /// Whether or not to call save/load on get/set }; diff --git a/libraries/AP_Common/c++.cpp b/libraries/AP_Common/c++.cpp index b769728097..d0f54b940f 100644 --- a/libraries/AP_Common/c++.cpp +++ b/libraries/AP_Common/c++.cpp @@ -13,6 +13,9 @@ void * operator new(size_t size) { +#ifdef AP_DISPLAYMEM + displayMemory(); +#endif return(calloc(size, 1)); } @@ -32,6 +35,9 @@ extern "C" void __cxa_pure_virtual() void * operator new[](size_t size) { +#ifdef AP_DISPLAYMEM + displayMemory(); +#endif return(calloc(size, 1)); } diff --git a/libraries/AP_EEProm/AP_EEProm.cpp b/libraries/AP_EEProm/AP_EEProm.cpp index a5e57fcfd9..9811be788b 100644 --- a/libraries/AP_EEProm/AP_EEProm.cpp +++ b/libraries/AP_EEProm/AP_EEProm.cpp @@ -17,6 +17,20 @@ */ #include +void AP_EEPromRegistry::print(BetterStream & stream) +{ + stream.printf("\nEEPROM Registry\n"); + for (int i=0;igetEntryParentName(), + (*this)[i]->getEntryName(), + (*this)[i]->getEntryId(), + (*this)[i]->getEntryAddress(), + (*this)[i]->getEntry()); + } +} + void AP_EEPromRegistry::add(AP_EEPromEntryI * entry, uint16_t & id, uint16_t & address, size_t size) { diff --git a/libraries/AP_EEProm/AP_EEProm.h b/libraries/AP_EEProm/AP_EEProm.h index 0bdca66e40..e8eb397b38 100644 --- a/libraries/AP_EEProm/AP_EEProm.h +++ b/libraries/AP_EEProm/AP_EEProm.h @@ -23,6 +23,7 @@ #include #include #include +#include /// The interface for data entries in the eeprom registry class AP_EEPromEntryI @@ -41,6 +42,9 @@ public: /// Pure virtual function for getting entry name. virtual const char * getEntryName() = 0; + /// Pure virtual function for getting entry parent name. + virtual const char * getEntryParentName() = 0; + /// Get the id of the variable. virtual uint16_t getEntryId() = 0; @@ -62,6 +66,9 @@ public: /// Add an entry to the registry void add(AP_EEPromEntryI * entry, uint16_t & id, uint16_t & address, size_t size); + /// Print on desired serial port + void print(BetterStream & stream); + private: uint16_t _newAddress; /// the address for the next new variable uint16_t _newId; /// the id of the next new variable @@ -79,8 +86,8 @@ class AP_EEPromVar : public AP_EEPromEntryI, public AP_Var { public: /// The default constrcutor - AP_EEPromVar(type data = 0, const char * name = "", bool sync=false) : - AP_Var(data,name,sync) + AP_EEPromVar(type data = 0, const char * name = "", const char * parentName = "", bool sync=false) : + AP_Var(data,name,parentName,sync) { eepromRegistry.add(this,_id,_address,sizeof(type)); } @@ -88,6 +95,7 @@ public: virtual void setEntry(float val) { this->setF(val); } virtual float getEntry() { return this->getF(); } virtual const char * getEntryName() { return this->getName(); } + virtual const char * getEntryParentName() { return this->getParentName(); } /// Get the id of the variable. virtual uint16_t getEntryId() { return _id; } diff --git a/libraries/AP_RcChannel/AP_RcChannel.cpp b/libraries/AP_RcChannel/AP_RcChannel.cpp index 216d70d0b6..e3dabfb9ec 100644 --- a/libraries/AP_RcChannel/AP_RcChannel.cpp +++ b/libraries/AP_RcChannel/AP_RcChannel.cpp @@ -14,6 +14,28 @@ #include "AP_RcChannel.h" #include +AP_RcChannel::AP_RcChannel(const char * name, const APM_RC_Class & rc, const uint8_t & ch, + const float & scale, const float & center, + const uint16_t & pwmMin, + const uint16_t & pwmNeutral, const uint16_t & pwmMax, + const uint16_t & pwmDeadZone, + const bool & filter, const bool & reverse) : + _name(name), + _rc(rc), + _ch(new AP_EEPROM_Uint8(ch,"CH",name)), + _scale(new AP_EEPROM_Float(scale,"SCALE",name)), + _center(new AP_EEPROM_Float(center,"CNTR",name)), + _pwmMin(new AP_EEPROM_Uint16(pwmMin,"PMIN",name)), + _pwmMax(new AP_EEPROM_Uint16(pwmMax,"PMAX",name)), + _pwmNeutral(new AP_EEPROM_Uint16(pwmNeutral,"PNTRL",name)), + _pwmDeadZone(new AP_EEPROM_Uint16(pwmDeadZone,"PDEAD",name)), + _pwm(0), + _filter(new AP_EEPROM_Bool(filter,"FLTR",name)), + _reverse(new AP_EEPROM_Bool(reverse,"REV",name)) + { + } + + void AP_RcChannel::readRadio() { // apply reverse uint16_t pwmRadio = APM_RC.InputCh(getCh()); @@ -23,8 +45,9 @@ void AP_RcChannel::readRadio() { void AP_RcChannel::setPwm(uint16_t pwm) { + //Serial.printf("pwm in setPwm: %d\n", pwm); //Serial.printf("reverse: %s\n", (getReverse())?"true":"false"); - + // apply reverse if(getReverse()) pwm = int16_t(getPwmNeutral()-pwm) + getPwmNeutral(); @@ -69,8 +92,8 @@ uint16_t AP_RcChannel::_positionToPwm(const float & position) { uint16_t pwm; - float p = position - getCenter(); //Serial.printf("position: %f\n", position); + float p = position - getCenter(); if(p < 0) pwm = p * int16_t(getPwmNeutral() - getPwmMin()) / getScale() + getPwmNeutral(); diff --git a/libraries/AP_RcChannel/AP_RcChannel.h b/libraries/AP_RcChannel/AP_RcChannel.h index 02d08bcd46..faac003159 100644 --- a/libraries/AP_RcChannel/AP_RcChannel.h +++ b/libraries/AP_RcChannel/AP_RcChannel.h @@ -24,27 +24,14 @@ public: const float & scale=45.0, const float & center=0.0, const uint16_t & pwmMin=1200, const uint16_t & pwmNeutral=1500, const uint16_t & pwmMax=1800, - const uint16_t & pwmDeadZone=100, - const bool & filter=false, const bool & reverse=false) : - _name(name), - _rc(rc), - _ch(new AP_EEPROM_Uint8(ch,createName("CH"))), - _scale(new AP_EEPROM_Float(scale,createName("SCALE"))), - _center(new AP_EEPROM_Float(center,createName("CNTR"))), - _pwmMin(new AP_EEPROM_Uint16(pwmMin,createName("PMIN"))), - _pwmMax(new AP_EEPROM_Uint16(pwmMax,createName("PMAX"))), - _pwmNeutral(new AP_EEPROM_Uint16(pwmNeutral,createName("PNTRL"))), - _pwmDeadZone(new AP_EEPROM_Uint16(pwmDeadZone,createName("PDEAD"))), - _pwm(0), - _filter(new AP_EEPROM_Bool(filter,createName("FLTR"))), - _reverse(new AP_EEPROM_Bool(reverse,createName("RVRS"))) - { - } + const uint16_t & pwmDeadZone=10, + const bool & filter=false, const bool & reverse=false); // set void readRadio(); void setPwm(uint16_t pwm); void setPosition(float position); + void setNormalized(float normPosition) { setPosition(normPosition*getScale()); } void mixRadio(uint16_t infStart); void setCh(const uint8_t & ch) { _ch->set(ch); } void setScale(const float & scale) { _scale->set(scale); } @@ -75,16 +62,6 @@ public: private: - // createName - const char * createName(char * str) - { - char * newName; - strcpy(newName,_name); - strcat(newName,"_"); - strcat(newName,str); - return (const char * )newName; - } - // configuration const char * _name; const APM_RC_Class & _rc; diff --git a/libraries/AP_RcChannel/examples/ServoSweep/ServoSweep.pde b/libraries/AP_RcChannel/examples/ServoSweep/ServoSweep.pde index f4dc4a4d05..f5ebc49537 100644 --- a/libraries/AP_RcChannel/examples/ServoSweep/ServoSweep.pde +++ b/libraries/AP_RcChannel/examples/ServoSweep/ServoSweep.pde @@ -5,6 +5,7 @@ */ +#define AP_DISPLAYMEM #include #include #include // ArduPilot Mega RC Library @@ -14,22 +15,21 @@ FastSerialPort0(Serial); // make sure this procees variable declarations // test settings -uint8_t nChannels = 1; +uint8_t nChannels = 8; // channel configuration -AP_RcChannel rc[] = +AP_RcChannel rcCh[] = { AP_RcChannel("ROLL",APM_RC,0,100.0), - /* AP_RcChannel("PITCH",APM_RC,1,45), AP_RcChannel("THR",APM_RC,2,100), AP_RcChannel("YAW",APM_RC,3,45), AP_RcChannel("CH5",APM_RC,4,1), AP_RcChannel("CH6",APM_RC,5,1), - AP_RcChannel("CH7",APM_tC,6,1), + AP_RcChannel("CH7",APM_RC,6,1), AP_RcChannel("CH8",APM_RC,7,1) - */ }; + // test position float testPosition = 0; int8_t testSign = 1; @@ -38,39 +38,43 @@ void setup() { Serial.begin(115200); Serial.println("ArduPilot RC Channel test"); + + eepromRegistry.print(Serial); // show eeprom map APM_RC.Init(); // APM Radio initialization + + for (int i=0;i 1) { - testPosition += testSign*.05; - if (testPosition > 1) - { - testPosition = 1; - testSign = -1; - } - else if (testPosition < -1) - { - testPosition = -1; - testSign = 1; - } + testPosition = 1; + testSign = -1; + } + else if (testPosition < -1) + { + testPosition = -1; + testSign = 1; } delay(500);