Memory Error

Hi, I wasn’t sure if it was appropriate to go into an old post or make a new one but from reading other posts I think I just have a stupid error in my code. I’m not exactly sure what I’m doing wrong but I keep getting a memory permission error. The goal of this code is just in autonomous for the indexer to run until the condition of the line sensor is met and to stop.

/*----------------------------------------------------------------------------*/
/*                                                                            */
/*    Module:       main.cpp                                                  */
/*    Author:       VEX                                                       */
/*    Created:      Thu Sep 26 2019                                           */
/*    Description:  Competition Template                                      */
/*                                                                            */
/*----------------------------------------------------------------------------*/

// ---- START VEXCODE CONFIGURED DEVICES ----
// ---- END VEXCODE CONFIGURED DEVICES ----

#include "vex.h"

using namespace vex;

// A global instance of competition
competition Competition;

  vex::motor LeftMotor = vex::motor( vex::PORT11,true );
  vex::motor BackLeftMotor = vex::motor( vex::PORT12,true );
vex::motor_group LDrive = vex::motor_group(LeftMotor,BackLeftMotor);
  vex::motor RightMotor = vex::motor( vex::PORT20,false );
  vex::motor BackRightMotor = vex::motor( vex::PORT19,false );
vex::motor_group RDrive = vex::motor_group(RightMotor,BackRightMotor);
vex::motor_group Drive = vex::motor_group(LeftMotor,BackLeftMotor,RightMotor,BackRightMotor);
  vex::motor Intake = vex::motor(vex::PORT13,true );
  vex::motor Intake2 = vex::motor(vex::PORT17,false);
vex::motor_group Intakes = vex::motor_group(Intake,Intake2);
  vex::motor Index = vex::motor(vex::PORT18,false );
  vex::motor Index2 = vex::motor(vex::PORT14,false);  
vex::motor_group Indexer = vex::motor_group(Index,Index2);
    vex::controller Controller1 = vex::controller(vex::controllerType::primary);
    vex::controller Controller2 = vex::controller(vex::controllerType::partner);
  triport ThreeWirePort = vex::triport( vex::PORT22 );
vex::line LineA = vex::line(Brain.ThreeWirePort.A);
vex::line LineB = vex::line(Brain.ThreeWirePort.B);

int noball;
int noball2;
int noball3;

void wait(float mSec){
vex::task::sleep(mSec);
}



void autosort(){
while (true ){
if (LineB.value(analogUnits::pct) <= noball - 30 ){
Indexer.stop();
}
else {
Index.spin(forward,100,pct);
Index2.spin(reverse,100,pct);
}

}
}
/*---------------------------------------------------------------------------*/
/*                          Pre-Autonomous Functions                         */
/*                                                                           */
/*  You may want to perform some actions before the competition starts.      */
/*  Do them in the following function.  You must return from this function   */
/*  or the autonomous and usercontrol tasks will not be started.  This       */
/*  function is only called once after the V5 has been powered on and        */
/*  not every time that the robot is disabled.                               */
/*---------------------------------------------------------------------------*/

void pre_auton(void) {

noball2 = LineA.value(analogUnits::pct);
noball3 = LineB.value(analogUnits::pct);


  vexcodeInit();

  // All activities that occur before the competition starts
  // Example: clearing encoders, setting servo positions, ...
}

/*---------------------------------------------------------------------------*/
/*                                                                           */
/*                              Autonomous Task                              */
/*                                                                           */
/*  This task is used to control your robot during the autonomous phase of   */
/*  a VEX Competition.                                                       */
/*                                                                           */
/*  You must modify the code to add your own robot specific commands here.   */
/*---------------------------------------------------------------------------*/

void autonomous(void) {

autosort();

//Drive.rotateFor(-800, vex::rotationUnits::raw, 70, vex::velocityUnits::pct,true);//Drives forward to turn

}

/*---------------------------------------------------------------------------*/
/*                                                                           */
/*                              User Control Task                            */
/*                                                                           */
/*  This task is used to control your robot during the user control phase of */
/*  a VEX Competition.                                                       */
/*                                                                           */
/*  You must modify the code to add your own robot specific commands here.   */
/*---------------------------------------------------------------------------*/

void usercontrol(void) {
  // User control code here, inside the loop
  while (1) {

    // This is the main execution loop for the user control program.
    // Each time through the loop your program should update motor + servo
    // values based on feedback from the joysticks.

    // ........................................................................
    // Insert user code here. This is where you use the joystick values to
    // update your motors, etc.
    // ........................................................................

    wait(20, msec); // Sleep the task for a short amount of time to
                    // prevent wasted resources.
  }
}

//
// Main will set up the competition functions and callbacks.
//
int main() {
  // Set up callbacks for autonomous and driver control periods.
  Competition.autonomous(autonomous);
  Competition.drivercontrol(usercontrol);
  autosort();


  // Run the pre-autonomous function.
  pre_auton();

  // Prevent main from exiting with an infinite loop.
  while (true) {
    wait(100, msec);
  }
}

It’s probably because Brain is defined elsewhere, and you are referencing it when creating LineA and LineB, use the ThreeWirePort you have just instantiated in the previous line.
change

  triport ThreeWirePort = vex::triport( vex::PORT22 );
vex::line LineA = vex::line(Brain.ThreeWirePort.A);
vex::line LineB = vex::line(Brain.ThreeWirePort.B);

to

  triport ThreeWirePort = vex::triport( vex::PORT22 );
vex::line LineA = vex::line(ThreeWirePort.A);
vex::line LineB = vex::line(ThreeWirePort.B);
5 Likes

Thank you, it got rid of the memory permission error so now I can get the code working.