Vex v5 custom Drive Train class memory error (memory error: 034143C0)

Hello, I am making a custom drive train class for my robot this year and some why every time I try to make a motor spin or something like that I get the 034143C0 memory error.

I have tried many solution and some why none of them are working.

here the code:

main.cpp:

Summary
#include "sys/_intsup.h"

#include "sys/_stdint.h"

#include "vex.h"

#include "DRIVE_TRAIN.h"

#include "vex_drivetrain.h"

#include "vex_global.h"

#include <string>

using namespace vex;

competition Competition;

bool IsAuto = false;

DRIVE_TRAIN Drive_train{0.008,0,0,PORT1,PORT2,PORT3,PORT4,PORT5,PORT6,PORT7,PORT8,PORT9};

const char * Auto1() {
  Brain.Screen.setCursor(1,1);
  Brain.Screen.clearScreen();
  //autonomus[selected].c_str();
  //Drive_train.Test();
  Drive_train.Test();
  Brain.Screen.print("hi");
  return "Done";
}

int whenStarted1() {
  Drive_train.Init();
  BumperA.pressed(PressDetect);
  return 0;
}

void VEXcode_driver_task() {
  // Start the driver control tasks....
  vex::task drive0(ondriver_drivercontrol_0);
  while(Competition.isDriverControl() && Competition.isEnabled()) {this_thread::sleep_for(10);}
  drive0.stop();
  return;
}

void VEXcode_auton_task() {
  // Start the auton control tasks....
  vex::task autotest(SelectAuto);
  while(Competition.isAutonomous() && Competition.isEnabled()) {this_thread::sleep_for(10);}
  autotest.stop();
  return;
}
int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vex::competition::bStopTasksBetweenModes = false;
  Competition.autonomous(VEXcode_auton_task);
  Competition.drivercontrol(VEXcode_driver_task);
  vexcodeInit();  
  whenStarted1();
}

DRIVE_TRAIN.cpp:

Summary
#include "math.h"
#include "stdio.h"
#include "sys/_stdint.h"
#include "time.h"
#include "vex.h"
#include "vex_global.h"
#include "vex_imu.h"
#include "MiniPID.h"
#include "vex_motor.h"
#include "vex_motorgroup.h"
#include "vex_task.h"
#include "DRIVE_TRAIN.h"

using namespace vex;

DRIVE_TRAIN::DRIVE_TRAIN(double P,double I,double D,int32_t PORT_FR,int32_t PORT_FL,int32_t PORT_RR,int32_t PORT_RL,int32_t PORT_ML,int32_t PORT_MR,int32_t PORT_CAT,int32_t PORT_INT,int32_t PORT_INERTIAL): DRIVE_PID(P,I,D), TURN_PID(P,I,D), MOTOR_FR(PORT_FR, ratio6_1, true), MOTOR_FL(PORT_FL, ratio6_1, false),MOTOR_RR(PORT_RR, ratio6_1, false),MOTOR_RL(PORT_RL),MOTOR_MR(PORT_MR, ratio6_1, false),MOTOR_ML(PORT_ML,ratio6_1, false),MOTOR_CAT(PORT_CAT,ratio6_1, false),MOTOR_INT(PORT_INT,ratio6_1, false),INERTIAL(PORT_INERTIAL),LEFT_SIDE(MOTOR_FL,MOTOR_RL,MOTOR_ML),RIGHT_SIDE(MOTOR_FR,MOTOR_RR,MOTOR_MR)
{
  this->P = P;
  this->I = I;
  this->D = D;
  
}


void DRIVE_TRAIN::Init(){
    /*DRIVE_PID=MiniPID(P,I,D);
    TURN_PID=MiniPID(P,I,D);
    MOTOR_FR = motor(PORT_FR, ratio6_1, true);
    MOTOR_FL = motor(PORT_FL, ratio6_1, false);
    MOTOR_RR = motor(PORT_RR, ratio6_1, false);
    MOTOR_RL = motor(PORT_RL, ratio6_1, true);
    MOTOR_ML = motor(PORT_ML, ratio6_1, true);
    MOTOR_MR = motor(PORT_MR, ratio6_1, false);
    MOTOR_CAT = motor(PORT_CAT, ratio6_1, false);
    MOTOR_INT = motor(PORT_INT, ratio6_1, false);
    INERTIAL = inertial(PORT_INERTIAL);*/
    this->RIGHT_SIDE = motor_group(MOTOR_RR,MOTOR_FR,MOTOR_MR);
    this->LEFT_SIDE = motor_group(MOTOR_RL,MOTOR_FL,MOTOR_MR);
    //Controller1 = controller(primary);
    OUTPUT_DRIVE;
    OUTPUT_TURN;
    DESIRED_DRIVE = 0;
    DESIRED_TURN = 0;
    SWITCH = false;
}

void DRIVE_TRAIN::Pid_loop(){
  while (1){
        if (PID_ENABLED == true){
            if (SWITCH == true){
              this->RIGHT_SIDE.setPosition(0,degrees);
              this->LEFT_SIDE.setPosition(0,degrees);
              SWITCH = false;
            }
            int DRIVE_AVERAGE = (RIGHT_SIDE.position(degrees)+RIGHT_SIDE.position(degrees))/2;
            int TURN_AVERAGE = Get_angle(); //(RIGHT_SIDE.position(degrees)-RIGHT_SIDE.position(degrees));
            OUTPUT_DRIVE = DRIVE_PID.getOutput(DRIVE_AVERAGE,DESIRED_DRIVE);
            OUTPUT_TURN = TURN_PID.getOutput(TURN_AVERAGE,DESIRED_TURN);
            this->RIGHT_SIDE.spin(forward,OUTPUT_DRIVE+OUTPUT_TURN,voltageUnits::volt);
            this->LEFT_SIDE.spin(forward,OUTPUT_DRIVE-OUTPUT_TURN,voltageUnits::volt);
        }
  }
}

void DRIVE_TRAIN::Start_drive(){
   while (true) {
    if (DRIVE_ENABLED){
        int LEFT_SIDE_SPEED = Controller1.Axis3.position();
        int RIGHT_SIDE_SPEED = Controller1.Axis2.position();
        RIGHT_SIDE.setVelocity(RIGHT_SIDE_SPEED, percent);
        RIGHT_SIDE.spin(forward);
        LEFT_SIDE.setVelocity(LEFT_SIDE_SPEED, percent);
        LEFT_SIDE.spin(forward);
    } else {
        LEFT_SIDE.stop();
        RIGHT_SIDE.stop();
    }
  }
}

void DRIVE_TRAIN::Drive_for(double DEGREE){
  DESIRED_DRIVE = DEGREE;
  SWITCH = true; // ?
}

void DRIVE_TRAIN::Turn_for(double ANGLE){
  DESIRED_TURN = ANGLE; //* 14.65;
  SWITCH = true; 
}

double DRIVE_TRAIN::Get_angle(){
  return INERTIAL.angle();
}

int DRIVE_TRAIN::Test(){
  this->RIGHT_SIDE.spinFor(forward, 900.0, degrees, true);
  return 1;
}

Thank you for helping if you do help.

Most likely you are using global class instances before they are created (ie. you are probably using motors or something before they exist). An explanation here.

5 Likes

I think the motor are created because I tried to update the .h file for the drive train class and some why it still doesn"t work and I’m unsure why

DRIVE_TRAIN.h:

#pragma once

#include "math.h"
#include "stdio.h"
#include "time.h"
#include "vex.h"
#include "vex_global.h"
#include "vex_imu.h"
#include "MiniPID.h"
#include "vex_motor.h"
#include "vex_task.h"

using namespace vex;

extern motor MOTOR_FR;
extern motor MOTOR_FL;
extern motor MOTOR_RR;
extern motor MOTOR_RL;
extern motor MOTOR_ML;
extern motor MOTOR_MR;
extern motor MOTOR_CAT;
extern motor MOTOR_INT;
extern inertial INERTIAL;
extern motor_group RIGHT_SIDE;
extern motor_group LEFT_SIDE;
extern controller Controller1;

struct DRIVE_TRAIN {
  int32_t PORT_FR;
  int32_t PORT_FL;
  int32_t PORT_RR;
  int32_t PORT_RL;
  int32_t PORT_ML;
  int32_t PORT_MR;
  int32_t PORT_TEST;
  int32_t PORT_CAT;
  int32_t PORT_INT;
  int32_t PORT_INERTIAL;

  double P;
  double I;
  double D;
  bool PID_ENABLED;
  bool DRIVE_ENABLED;

  DRIVE_TRAIN(double P, double I,double D,int32_t PORT_FR,int32_t PORT_FL,int32_t PORT_RR,int32_t PORT_RL,int32_t PORT_ML,int32_t PORT_MR,int32_t PORT_CAT,int32_t PORT_INT,int32_t PORT_INERTIAL);

  void Pid_loop();

  void Init();

  void Start_drive();

  void Drive_for(double DEGREE);

  void Turn_for(double ANGLE);

  double Get_angle();

  int Test();

  private:
    MiniPID DRIVE_PID;
    MiniPID TURN_PID;
    motor MOTOR_FR;
    motor MOTOR_FL;
    motor MOTOR_RR;
    motor MOTOR_RL;
    motor MOTOR_ML;
    motor MOTOR_MR;
    motor MOTOR_CAT;
    motor MOTOR_INT;
    inertial INERTIAL;
    motor_group RIGHT_SIDE;
    motor_group LEFT_SIDE;
    controller Controller1;
    double OUTPUT_DRIVE;
    double OUTPUT_TURN;
    double DESIRED_DRIVE;
    double DESIRED_TURN;
    bool SWITCH;
};

It’s hard to tell for sure, with just the amount of code you’ve posted, but one recommendation I might make would be to simplify your constructor to the simplest possible thing. Remove everything but the P, I, and D values from the constructors parameters, and move those other assignments, you have for your other functions down into your init function, or somewhere else that you can then explicitly call.

This will allow for your class to get fully constructed, and all necessary objects created first, before you’re actually trying to use anything that may not have actually been created in memory yet.