Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Main 'entry point' / threading issue


  • Please log in to reply
6 replies to this topic

#1 ncsteinb

ncsteinb

    Member

  • Members
  • PipPip
  • 27 posts

Posted 05 March 2013 - 07:07 PM

Hello all, 

 

I am trying to get this program to work for me, but since i'm a noob, I'm probably misunderstanding something here. 

I am getting an error saying "An object reference is required for the non-static field, method, or property 'NetduinoPlusApplication1.Program.Inputs()' " and the same with "...Program.Pressure()"

 

I don't know how to fix this. My code is below. Thanks.

 

namespace NetduinoPlusApplication1{    public class Program    {           double VAvgi = 0;        double VAvgj = 0;         double DeltaVAvg;        double VAvgOut;        double Dev = 0.01;        double tempC;        double Rho;        double g = 9.81;        double calOffset;        double PCorrect;        double PRead;        double calVol;        double calPress;        double V;        double h;        public static void Main()        {            Thread t1 = new Thread(Inputs);            t1.Start();            Thread t3 = new Thread(Pressure);            t3.Start();        }                public void Inputs()        {            InputPort ReadButton = new InputPort(Pins.GPIO_PIN_D0, false, Port.ResistorMode.PullUp);            InputPort CalButton = new InputPort(Pins.GPIO_PIN_D1, false, Port.ResistorMode.PullUp);            bool ReadState = false;            bool CalState = false;            calVol = 1.000;      // 1000mL or 1L                        while (true)            {                ReadState = !ReadButton.Read();                CalState = !CalButton.Read();                if (ReadState == true)                 {                    if (DeltaVAvg <= Dev)                    {                        Debug.Print("Value read");                        VAvgj = VAvgOut;                        Thread.Sleep(2000);                        Debug.Print("Calculated volume of sample is" + VAvgOut);                        Thread.Sleep(Timeout.Infinite);                        //Need some way to reset to measure another sample or remeasure.... like a press Enter to Continue or something....                    }                    else                    {                    }                }                if (CalState == true)                {                    if (DeltaVAvg <= Dev)                    {                        Debug.Print("Calibrating...");                        calVol = Rho * g * (calVol / (4 * System.Math.PI));                        calOffset = calVol - PRead;                        Debug.Print("Calibrated");                    }                    else                    {                        Debug.Print("Failed. Please wait till settled");                    }                }                else                {                }                Thread.Sleep(1000);                            }        }                            public void Pressure()        {            AnalogInput PT = new AnalogInput(Pins.GPIO_PIN_A1);            AnalogInput Tmp36 = new AnalogInput(Pins.GPIO_PIN_A0);            OutputPort GLed = new OutputPort(Pins.GPIO_PIN_D2, true);            OutputPort RLed = new OutputPort(Pins.GPIO_PIN_D3, true);                        int sampleSize = 10;            Queue rawQueue = new Queue();            Queue avgQueue = new Queue();                        while (true)            {                V = Tmp36.Read() * 3.3 / 1024;                tempC = (100 * V) - 50;                Rho = -0.0055 * ((tempC) * (tempC)) + 0.0157 * (tempC) + 1000.1;                                PRead = PT.Read() * 5 / 1024;                PCorrect = PRead + calOffset;                h = PCorrect / (Rho * g);                V = 4 * System.Math.PI * h;                while(rawQueue.Count != sampleSize)                {                    rawQueue.Enqueue(V);                }                while(rawQueue.Count == sampleSize)                {                    rawQueue.Enqueue(V);                    rawQueue.Dequeue();                    double sum = 0;                    foreach (double d in rawQueue)                    {                        sum += d;                    }                                        VAvgi = VAvgj;                    VAvgj = sum/sampleSize;                }                double diffAvg;                                                diffAvg = (VAvgi - VAvgj);                if (diffAvg < 0)                {                     DeltaVAvg = diffAvg * -1;                }                                 else                {                    DeltaVAvg = diffAvg;                }                                  if (DeltaVAvg <= Dev)                {                    GLed.Write(!true);                    Thread.Sleep(700);                    GLed.Write(!false);                    Thread.Sleep(300);                }                else                {                    RLed.Write(!true);                    Thread.Sleep(200);                    RLed.Write(!false);                    Thread.Sleep(800);                }                                            }        }    }}

 

 



#2 NooM

NooM

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationAustria

Posted 05 March 2013 - 07:30 PM

public static void Inputs()

{ ... }

 

will do the trick.

id also suggest putting a: Thread.Sleep(Timeout.Infinite); after you thread initialization code in the Main()



#3 ncsteinb

ncsteinb

    Member

  • Members
  • PipPip
  • 27 posts

Posted 05 March 2013 - 07:36 PM

I will do the Thread.Sleep(Timeout.Infinite); Cleanliness right?

 

When I add the 'static' modifier, I get the 'object reference' error with all variables within that method that were declared in the class.

 

Any thoughts?



#4 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 05 March 2013 - 07:45 PM

When I add the 'static' modifier, I get the 'object reference' error with all variables within that method that were declared in the class.

  Static methods can access only static members (fields, properties etc.), so you'd need to either mark everything as static, or move it to a class (making everything non-static).

 

Edit: You could probably move variables into methods, so they are declared at the top of the method which uses them. Only ones that you need to access from both methods would need to be static (and preferably guarded with some synchronization mechanism, because they would be accessed from different threads).


Edited by CW2, 05 March 2013 - 07:50 PM.


#5 ziggurat29

ziggurat29

    Advanced Member

  • Members
  • PipPipPip
  • 244 posts

Posted 05 March 2013 - 11:24 PM

The 'Main' method is a static method.  It cannot access the Program member vars (unless they were static, too), and also it cannot use the non-static functions (in this case the thread functions to start up the thread).  If you make Main instantiate an instance of the Program object, then you can proceed.  Like this:

 

namespace NetduinoApplication1{    public class Program    {        //XXX ... various instance vars        public static void Main()        {             //XXX make an instance of the Program, and forward execution            Program pgm = new Program();            pgm.objectifiedMain();            //XXX as Noom points out, an infinite sleep is typically done here            Thread.Sleep(Timeout.Infinite);        }        void objectifiedMain()        {            Thread t1 = new Thread(Inputs);            t1.Start();            Thread t3 = new Thread(Pressure);            t3.Start();        }        void Inputs()        {            //XXX ... interesting stuff in a thread that can access the member vars        }        void Pressure()        {            //XXX ... interesting stuff in a thread that can access the member vars        }    }}

-dave



#6 NooM

NooM

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationAustria

Posted 05 March 2013 - 11:34 PM

sorry i just havent really looked at his whole code, but the issues sounded like missing statics.

yeah the beast way is make your main class not static.

 

 

ziggurat made a very good sample, but i have some suggestions:

namespace NetduinoApplication1{    public class Program    {        //XXX ... various instance vars                // place this at the end of your file here, normally you not gonna change         //anything in main anymore.        public static void Main()         {            //XXX make an instance of the Program, and forward execution            Program pgm = new Program();            while(true)            {              pgm.Update();            }        }        public Program()        {            Thread t1 = new Thread(Inputs);            t1.Start();            Thread t3 = new Thread(Pressure);            t3.Start();        }        public void Update() // loops.        {          // thread.sleep ...        }        void Inputs()        {            //XXX ... interesting stuff in a thread that can access the member vars        }        void Pressure()        {            //XXX ... interesting stuff in a thread that can access the member vars        }    }}


#7 ncsteinb

ncsteinb

    Member

  • Members
  • PipPip
  • 27 posts

Posted 06 March 2013 - 01:15 PM

Thanks guys! You helped a lot! My program works now.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.