Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在另一个类中使用对象(需要参数)及其方法_C++_Robotics_Robot_Beagleboneblack - Fatal编程技术网

C++ 在另一个类中使用对象(需要参数)及其方法

C++ 在另一个类中使用对象(需要参数)及其方法,c++,robotics,robot,beagleboneblack,C++,Robotics,Robot,Beagleboneblack,我正试图用BeagleBone黑色为全方位机器人编程。我发现BlackLib库在控制管脚方面非常有效,但是我在将它集成到更大的方案中时遇到了一些问题。如果我简单地创建与管脚关联的对象,并将它们设置为高或低(或介于两者之间),我就能够根据需要控制电机。但当我试图创建一个包含BlackLib对象的对象时,我遇到了困难。在我看来,一个很好的方法是创建一个包含GPIO和PWM BlackLib对象的motor对象。然后,我可以创建一个函数来轻松设置功率和方向 rightMotor(50); //Shou

我正试图用BeagleBone黑色为全方位机器人编程。我发现BlackLib库在控制管脚方面非常有效,但是我在将它集成到更大的方案中时遇到了一些问题。如果我简单地创建与管脚关联的对象,并将它们设置为高或低(或介于两者之间),我就能够根据需要控制电机。但当我试图创建一个包含BlackLib对象的对象时,我遇到了困难。在我看来,一个很好的方法是创建一个包含GPIO和PWM BlackLib对象的motor对象。然后,我可以创建一个函数来轻松设置功率和方向

rightMotor(50); //Should set the power to 50% in one direction
我已经完成了一些工作,但是在访问BlackLib中的对象中的函数时遇到了困难,这些对象位于我自己的Motors类中

这是我正在使用的代码

#include "BlackLib/BlackLib.h"

struct Motors
{
  BlackPWM* pwm;
  BlackGPIO* gpio;

  void drive(int power)
  {
    if(power >= 0)
    {
      gpio.setValue(low);
      //gpio.setValue(low); generates the error described below. I'm not familiar enough with the intricacies of pointers to know how to handle this
      //motors.cpp: In member function ‘void Motors::drive(int)’:
      //motors.cpp:15:10: error: request for member ‘setValue’ in ‘((Motors*)this)->Motors::gpio’, which is of non-class type ‘BlackGPIO*’
//      pwm.setDutyPercent(power);
    }
    else
    {
//      gpio.setValue(high);
//      pwm.setDutyPercent(100+power);
    }
  }
};

int main()
{
  struct Motors rightMotor;
  rightMotor.pwm = new BlackPWM(P9_16);
  rightMotor.gpio = new BlackGPIO(GPIO_49,output);

  //Give the BeagleBone a little time to create the objects
  usleep(10000);

  rightMotor.pwm.setPeriodTime(500000);
  //I will eventually need to set the period time but I'm not sure how. I'm guessing this is the incorrect syntax
  //Ideally, I would create a function so that I could simply write rightMotor.setPeriodTime(500000);

  rightMotor.drive(50);
  sleep(1);
  rightMotor.drive(0);
}
如果我完全偏离了底线,有更好的方法,请告诉我。我的最终目标是能够轻松控制多个电机。最后,我想创建函数和类,例如

robot.turn(30);


获得了阿拉斯加费尔贝克斯大学计算机科学教授的以下解决方案。现在一切正常

struct Motors
{
        BlackPWM pwm;
        BlackGPIO gpio;

        Motors(pwm_pin_name pwmPin,gpio_name gpioPin) //motor constructor parameters
                : pwm(pwmPin),gpio(gpioPin,output) //member initializer list
        {
                pwm.setPeriodTime(500000);
        }

        void drive(int power)
        {
                if(power >= 0)
                {
                        gpio.setValue(low);
                        pwm.setDutyPercent(power);
                }
                else
                {
                        gpio.setValue(high);
                        pwm.setDutyPercent(100+power);
                }
        }
};

int main()
{
        //User interface pins
        BlackGPIO button(GPIO_44,input);

        Motors rightMotor(P9_16,GPIO_49);

        while(!button.isHigh())
        {
                rightMotor.drive(0);
        }

        //The BeagleBone needs a little time to create the PWM object
        usleep(10000);

        rightMotor.drive(-50);

        sleep(1);

        rightMotor.drive(0);
}

你对C++有什么经验?您在这里犯了一些基本错误(比如通过
而不是
->
访问指针的成员函数,或者封装不好)。在处理此类项目之前,我强烈建议您先了解一下C++/OOP。
struct Motors
{
        BlackPWM pwm;
        BlackGPIO gpio;

        Motors(pwm_pin_name pwmPin,gpio_name gpioPin) //motor constructor parameters
                : pwm(pwmPin),gpio(gpioPin,output) //member initializer list
        {
                pwm.setPeriodTime(500000);
        }

        void drive(int power)
        {
                if(power >= 0)
                {
                        gpio.setValue(low);
                        pwm.setDutyPercent(power);
                }
                else
                {
                        gpio.setValue(high);
                        pwm.setDutyPercent(100+power);
                }
        }
};

int main()
{
        //User interface pins
        BlackGPIO button(GPIO_44,input);

        Motors rightMotor(P9_16,GPIO_49);

        while(!button.isHigh())
        {
                rightMotor.drive(0);
        }

        //The BeagleBone needs a little time to create the PWM object
        usleep(10000);

        rightMotor.drive(-50);

        sleep(1);

        rightMotor.drive(0);
}