Pages: [1]   Go Down
Author Topic: Bizarre problem when creating Arduino library  (Read 906 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello everyone.
I'm really stumped - I'm having this really strange problem when I try to create an Arduino library.

I have a header file - MpptLib.h - and a .cpp file - MpptLib.cpp.

My top-level design file is called "Manager." It uses the functions in the MpptLib library.

So, the weird problem:
I try to create an instance of the class MpptLib, called mpptlib.  Then, I try to call the function read_data() in mpptlib.
So I type:
MpptLib mpptlib();
mpptlib.read_data();

BUT, even though I haven't even hit "Verify" and tried to compile, the software freaks out. The line "mpptlib.read_data();" and everything below it disappears, and I get a constant flow of error messages.
Here are just a few of them:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 11
at processing.app.syntax.JEditTextArea._offsetToX(JEditTextArea.java:552)
at processing.app.syntax.JEditTextArea.scrollTo(JEditTextArea.java:447)
at processing.app.syntax.JEditTextArea.scrollToCaret(JEditTextArea.java:412)
at processing.app.syntax.JEditTextArea.select(JEditTextArea.java:1177)
at processing.app.syntax.JEditTextArea$DocumentHandler.removeUpdate(JEditTextArea.java:1993)
at javax.swing.text.AbstractDocument.fireRemoveUpdate(AbstractDocument.java:242)
at processing.app.syntax.SyntaxDocument.fireRemoveUpdate(SyntaxDocument.java:164)
at javax.swing.text.AbstractDocument.handleRemove(AbstractDocument.java:607)


As soon as I backspace on the line mpptlib.read_data(), the error messages stop coming.

The code for my Manager file (the file that uses the MpptLib library) is:
Code:
//Manager - Top-Level
//April 1, 2010

#include "MpptLib.h"

void setup()
{
  Serial.begin(9600);            //Open serial port
}

void loop()
{
  MpptLib mpptlib();
  mpptlib.read_data(); //(I can't actually type this line without the software freaking out)
}

Could someone please help me out? I'm pretty new to OOP and completely new to C++.

Thanks so much!
« Last Edit: April 04, 2010, 10:37:44 pm by modom » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is the code for my header file:
Code:
/*
 *  MpptLib.h
 *  
 *
 *  Created by J . on 4/4/10.
 *  Copyright 2010 __MyCompanyName__. All rights reserved.
 *
 */

#ifndef MpptLib_h
#define MpptLib_h

#include "WProgram.h"

class MpptLib
{
      public:
            MpptLib();
            int read_adc(int channel);
            void set_pwm_duty();
            void run_charger();
            void read_data();
            void print_data();      
      private:
            int analogPin_solvolts;  // Analog pin 2 - panel voltage
            int analogPin_solamps;  // Analog pin 6 - panel current (suspect damage on pin 3)
            int analogPin_battvolts;  //Analog pin 4 - battery voltage
            float sol_volts_resolution; //solar volts as read by the a/dc converter
            float sol_volts_divided;   //solar volts at output of voltage divider
            float sol_volts_actual;   // solar volts after being scaled back to actual panel voltage
            float sol_amps_resolution;  //solar amps as read by a/d converter
            float sol_volts_csensor;    //output voltage seen by current sensor; this will be scaled to the value of output current
            float sol_amps_actual;      //solar amps after being scaled back to actual value of current from solar panel
            float sol_watts;      //current value of solar watts
            float old_sol_watts;     //value of solar watts on previous iteration of loop
            float bat_volts_resolution;    //battery volts as read by the a/d converter
            float bat_volts_divided;       //battery voltage at output of voltage divider
            float bat_volts_actual;        //actual battery voltage
            int pwm;
            int delta;
            enum charger_mode {off, on, bulk, bat_float} charger_state;    // enumerated variable that holds state for charger state machine
};


#endif
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And here is the code for my .cpp file:
Code:
/*
 *  MpptLib.cpp
 *  
 *
 *  Created by J . on 4/4/10.
 *  Copyright 2010 __MyCompanyName__. All rights reserved.
 *
 */
// test 6.
// right now, test 6 has been tested and it does pwm, does mosfet shut down successfully, and contains the working switch case statement.

#include "WProgram.h"
#include "MpptLib.h"

//------------------------------------------------------------------------------------------------------
//variable definitions
//------------------------------------------------------------------------------------------------------

      #define SOL_VOLTS_CHAN 5          //ADC channel to read voltage from solar panel
      #define SOL_AMPS_CHAN 6           //ADC channel to read current from solar panel // changed to 6 (suspect damage on pin 3)
      #define BAT_VOLTS_CHAN 7          //ADC channel to read battery voltage
      #define SD_PIN 22                 //digital pin 22 controls SHUTDOWN function of mosfet driver
      #define AVG_NUM 15                //the average number of readings is 15 before we actually scale
      #define SOL_VOLTS_SCALE1 0.0049   //the value to scale the DIGITAL (0-1023) reading of the panel voltage by to get the Vo (from voltage divider)
      #define SOL_VOLTS_SCALE2 5.19   //the average ratio of Vin/Vo from voltage divider calculation; use this to scale Vo back up to Vin (actual panel voltage)
      #define SOL_AMPS_SCALE1 0.07143       // JOSH CHANGED THIS -- changed sense resistor values
      #define BAT_VOLTS_SCALE1 3.478607 //5.828607        //the scale factor to convert voltage seen at the output of the voltage divider for battery charge back to actual battery voltage
      #define PWM_MAX 242               //maximum pwm duty cycle = 95%
      #define PWM_MIN 65                //minimum pwm duty cycle = 60%
      #define PWM_START 229              //starting pwm duty cycle = 90%
      #define LOW_SOL_WATTS 0.1
      #define MIN_SOL_VOLTS 13
      #define LOW_BAT_VOLTS 11.8
      #define HIGH_BAT_VOLTS 12.9
      #define TURN_OFF_MOSFETS digitalWrite(MOSFET_SD, LOW)  //when SD pin is asserted, mosfet driver is off
      #define TURN_ON_MOSFETS digitalWrite(MOSFET_SD, HIGH)    //when SD pin is low, mosfet driver is on
      #define MOSFET_SD 22      // digital pin 22 is the mosfet shutdown pin


//------------------------------------------------------------------------------------------------------
//global variables
//------------------------------------------------------------------------------------------------------
int analogPin_solvolts = 5;  // Analog pin 2 - panel voltage
int analogPin_solamps = 6;  // Analog pin 6 - panel current (suspect damage on pin 3)
int analogPin_battvolts = 7;  //Analog pin 4 - battery voltage
float sol_volts_resolution; //solar volts as read by the a/dc converter
float sol_volts_divided;   //solar volts at output of voltage divider
float sol_volts_actual;   // solar volts after being scaled back to actual panel voltage
float sol_amps_resolution;  //solar amps as read by a/d converter
float sol_volts_csensor;    //output voltage seen by current sensor; this will be scaled to the value of output current
float sol_amps_actual;      //solar amps after being scaled back to actual value of current from solar panel
float sol_watts;      //current value of solar watts
float old_sol_watts = 0;     //value of solar watts on previous iteration of loop
float bat_volts_resolution;    //battery volts as read by the a/d converter
float bat_volts_divided;       //battery voltage at output of voltage divider
float bat_volts_actual;        //actual battery voltage
int pwm;
int delta = 2;
enum charger_mode {off, on, bulk, bat_float} charger_state;    // enumerated variable that holds state for charger state machine

//------------------------------------------------------------------------------------------------------
//powerup method;; this happens automatically on powerup but all hast to do with pwm initialization
//------------------------------------------------------------------------------------------------------

MpptLib::MpptLib()
{
      /*Serial.begin(9600);          //open the serial port*/
      pinMode(MOSFET_SD, OUTPUT);    //set mosfet shutdown as an output
      pinMode(10, OUTPUT);      //this actually corresponds to TIMER 2 CHANNEL A (change PWM with OCR2A)
      TURN_ON_MOSFETS;
      TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
      TCCR2B = _BV(CS20);
      OCR2A = PWM_START;    //start with an 80% duty cycle
      pwm = OCR2A;    //set pwm equal to the value in OCR2A, which is the pwm resolution from 0-255.
      charger_state = bulk;
      pwm = OCR2A;
      Serial.println("SETUP HAPPENED");
}


//------------------------------------------------------------------------------------------------------
// This routine reads and averages the analog inputs for this system, solar volts, solar amps and
// battery volts. It is called with the adc channel number (pin number) and returns the average adc
// value as an integer.
//
// Reads the solar volts and amps.
// Called with the adc channel number (pin number)
// Returns the current or voltage values -- over 15 readings and 12.5 microseconds apart from one another -- averaged.
//------------------------------------------------------------------------------------------------------
int MpptLib::read_adc(int channel)
{  
 int sum = 0;
 int temp;
  int i;
  
  for (i=0; i<AVG_NUM; i++) {            // loop through reading raw adc values AVG_NUM number of times (15 times)
    temp = analogRead(channel);          // read the input pin  
    sum += temp;                       // store sum for averaging
    delayMicroseconds(30);              // pauses for 30 microseconds  
  }
 return(sum / AVG_NUM);                // divide sum by AVG_NUM to get average and return it
}
//------------------------------------------------------------------------------------------------------
//set the pwm duty cycle - this checks if pwm is within limits.
//if pwm is within limits, it goes to the bulk state.
//------------------------------------------------------------------------------------------------------
void MpptLib::set_pwm_duty(void)
{
  //code goes here                              
}            
//------------------------------------------------------------------------------------------------------
//run charger
//------------------------------------------------------------------------------------------------------
void MpptLib::run_charger(void)
{
  //code goes here
}
    
 //------------------------------------------------------------------------------------------------------
void MpptLib::read_data(void)
{
  //read values
  sol_volts_resolution = read_adc(SOL_VOLTS_CHAN);
  sol_amps_resolution = read_adc(SOL_AMPS_CHAN);
  //calculate solar panel voltage
  sol_volts_divided = sol_volts_resolution * (float)SOL_VOLTS_SCALE1 + 0.015;    //multiplies resolution by 0.0049 to get Vo of divider and adds scale factor to compenstae for regulator
  sol_volts_actual = sol_volts_divided * (float)SOL_VOLTS_SCALE2;                //multiplies Vo by 4.9286 (average ratio of Vin/Vout) to get ACTUAL PANEL VOLTAGE
  //calculates solar panel current
  sol_volts_csensor = sol_amps_resolution * (float)SOL_VOLTS_SCALE1;
  sol_amps_actual = sol_volts_csensor * (float)SOL_AMPS_SCALE1;                  //calculates voltage (and proportional current) from the analog input pin representing solar panel current
  //calculates solar panel power
  sol_watts = sol_volts_actual * sol_amps_actual;
  bat_volts_resolution = read_adc(BAT_VOLTS_CHAN);
  bat_volts_divided = bat_volts_resolution * (float)SOL_VOLTS_SCALE1;
  bat_volts_actual = bat_volts_divided * (float)BAT_VOLTS_SCALE1;
}
//----------------------------------------------------------------------------------------------------------
void MpptLib::print_data(void)
{
  //code here
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46090
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
 MpptLib mpptlib();

What is this line trying to do? You have a class name followed by a function call, but there is no function of that name defined anywhere.

If you are trying to define an instance of the class MpptLib, lose the ().
Logged

CT, USA
Offline Offline
Sr. Member
****
Karma: 2
Posts: 446
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yeah, the variable declaration definitely has some issue. But the error message seemed to have originated from the IDE? If so, it might indicate that there's some deficiency in parsing the code.
Logged

Minnesota USA
Offline Offline
Sr. Member
****
Karma: 1
Posts: 323
Made it mahself outta sand 'n wahr.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I would try a small change in the names used. I suspect some odd coincidence with something internal to the IDE. Just a guess, but easy enough to check..

If changing the names a little works, I would just refer it to whoever maintains the IDE and get on with writing my code.
Logged

Pages: [1]   Go Up
Jump to: