Wrong motor starts spinning

I’m new to VEXcode (V5 system in general), and I’m getting very confused by this problem. When I run the following code (not the exact layout, but you get the idea)
, the wrong motor starts moving. I have a few component namespaces with a similar problem, where any attempt at moving a motor_group runs the same one motor (port 1 I believe) that isn’t supposed to run. Here are the relevant pieces of code:

// Header
using vex::motor_group;

namespace robot {
    namespace Arm {
        extern std::unique_ptr<motor_group> motors;

        void init();
        std::unique_ptr<motor_group> makeMotors();
    };
}

// Implementation
using vex::motor;
using vex::motor_group;
using vex::gearSetting;
using vex::velocityUnits;

namespace robot {
    namespace Arm {
        std::unique_ptr<motor_group> motors;

        void init() {
            motors = makeMotors();
        }

        std::unique_ptr<motor_group> makeMotors() {
            motor armMotor(vex::PORT11, gearSetting::ratio36_1, false);
            return std::unique_ptr<motor_group>(new motor_group(armMotor));
        }
    }    
}

// main.cpp
robot::Arm::init();
robot::Arm::motors->setVelocity(100, vex::velocityUnits::pct);
while (true) {
	robot::Arm::motors->spin(vex::directionType::fwd);
}

Maybe I’m missing something, but could you share some more info about the vex::motor_group class? I was unable to find any reference to it in the docs or in VEXcode.

If it’s a class you wrote yourself, sharing the source for the class would also be helpful in debugging your program.

Hello. Its a class available in VEXcode only for now, that’s why its not in the docs (probably will be documented when VEXcode -> version 1.0).

Hmm, yes, I missed that. I looked through the motor_group header file and all your calls to those functions looked reasonable. The next thing I would try is to write the simplest possible program using the motor_group class, something like this (with the standard vex.h):

/*----------------------------------------------------------------------------*/
/*                                                                            */
/*    Module:       main.cpp                                                  */
/*    Author:       john                                                      */
/*    Created:      Thu Oct 03 2019                                           */
/*    Description:  Simple motor_group test                                   */
/*                                                                            */
/*----------------------------------------------------------------------------*/
#include "vex.h"
#include "vex_motorgroup.h"

using namespace vex;

// define your global instances of motors and other devices here
vex::motor myMotor(vex::PORT11);

//define the motor group:
vex::motor_group myGroup(myMotor);

int main() {
    myGroup.setVelocity(100.0, vex::velocityUnits::pct);

    while(1) {
        myGroup.spin(vex::directionType::fwd);
        this_thread::sleep_for(10);
    }
}
1 Like

Okay, I’ll try and get that working. My guts are telling me that all that fiddling with smart pointers is screwing something up :sweat_smile:

1 Like

Ok, I’ve opted to not use smart pointers (which now I know aren’t supposed to be used everywhere haha).

2 Likes