Main Site Documentation

The name 'pwmPhaseB1' does not exist in the current context


#1

hi every one

I work with a projeckt where I need to use PWM and ADC channal .

The project works, but would like to reuse class to another motor.

This is probably a beginner’s question. But how can I do this without error

The name ‘pwmPhaseB1’ does not exist in the current context


MotorDriver MotorPan = new MotorDriver(4,6,5,7,9,20000,false); // in the main 
...
MotorPan.Drive(true); // use the drive algoritme

class MotorDriver
{

    public MotorDriver(int pinPhaseA1, int pinPhaseA2, int pinPhaseB1, int pinPhaseB2, int pinVref, int Freq, bool Invert)
    {
            PWM pwmPhaseA1 = new PWM((Cpu.PWMChannel)pinPhaseA1, Freq, 0.0, Invert);
            PWM pwmPhaseA2 = new PWM((Cpu.PWMChannel)pinPhaseA2, Freq, 0.0, Invert);
            PWM pwmPhaseB1 = new PWM((Cpu.PWMChannel)pinPhaseB1, Freq, 0.0, Invert);
            PWM pwmPhaseB2 = new PWM((Cpu.PWMChannel)pinPhaseB2, Freq, 0.0, Invert);
            PWM pwmVref = new PWM((Cpu.PWMChannel)pinVref, 100000, 0.0, Invert);
    }


    public void Drive(bool dir)
    {
...
            pwmPhaseA1.DutyCycle = pwmValues[0];
            pwmPhaseA2.DutyCycle = pwmValues[1];
            pwmPhaseB1.DutyCycle = pwmValues[2];
            pwmPhaseB2.DutyCycle = pwmValues[3];

}
}


#2

@ andre.m

Yes it is true, but Drive and MotorDriver are in the same class.

How do at get access to the pwmPhase* in the other methods, that are in the same class?

:think:


#3

@ andre.m

Thanks for your reply :slight_smile:

when i try your example in my code, are there no error from the IDE.

But the program crash on the G120HDR, first time there are a PWM DutyCycle update.
Following exception:

Do you know why this is happens???


#4

Hi again :wink:

I have worked with it for several hours without success.

I chose to create a new project, where I have only made ​​pwm setup and dutycycle update.

But the same exception appeared.


using System;
using System.Threading;
using System.IO;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHI.Hardware.G120;
using GHI.Premium;
using GHI.Premium.System;


namespace PWMsetup_test
{
    public class Program
    {
        public static void Main()
        {
            PWM_setup MotorPan = new PWM_setup(0, 20000, false);
            while (true)
            {
                MotorPan.Update();
            }
        }
    }
}
 

namespace PWMsetup_test
{
    class PWM_setup
    {

        PWM pwmPhaseA1;

        public PWM_setup(int pinPhaseA1, int Freq, bool Invert)
        {
            PWM pwmPhaseA1 = new PWM((Cpu.PWMChannel)pinPhaseA1, Freq, 0.0, Invert);
        }
        public void Update()
        {
            pwmPhaseA1.DutyCycle = 0.2;
            Thread.Sleep(100);
            pwmPhaseA1.DutyCycle = 0.8;
            Thread.Sleep(100);
        }
    }
}

Is there a similar way to do this?


#5

first pwm update


#6

It seems like there are no access in “update”

Look at comment in code


        PWM pwmPhaseA1;
        public PWM_setup(int pinPhaseA1)
        {
            PWM pwmPhaseA1 = new PWM((Cpu.PWMChannel)pinPhaseA1, 20000, 0.0, false);    // =0.0
            pwmPhaseA1.DutyCycle = 0.5;     // =0.5
        }


        public void Update()
        {
            pwmPhaseA1.DutyCycle = 0.2;   // = NULL and break
            Thread.Sleep(100);
            pwmPhaseA1.DutyCycle = 0.8;
            Thread.Sleep(100);


        }


#7

The PWM you declared in PWM_setup shadowed the global PWM inside the function, so the global one was never set, only a temporary local one was. The below code fixes that.


PWM pwmPhaseA1;
         public PWM_setup(int pinPhaseA1)
         {
             pwmPhaseA1 = new PWM((Cpu.PWMChannel)pinPhaseA1, 20000, 0.0, false);    // =0.0
             pwmPhaseA1.DutyCycle = 0.5;     // =0.5
         }


         public void Update()
         {
             pwmPhaseA1.DutyCycle = 0.2;   // = NULL and break
             Thread.Sleep(100);
             pwmPhaseA1.DutyCycle = 0.8;
             Thread.Sleep(100);


         }


#8

@ John
yes just find out 10 min ago :whistle:

Tak for all your time and help
@ andre.m
@ John