Automatic Braking System

Hey y’all, I’m working on a project right now that involves using the vex distance sensors as a way to create automatic braking. Right now I have 2 issues: When the robot gets close enough to the wall, it just stops moving. This is likely due to me setting the speed based on division not subtraction. and the second issue is the robot sometimes doesn’t stop at all. My main goal is for the robot to slowly slow down as it gets closer to the wall, but I’m unsure how to have the robot slowdown without impeding the ability to drive backward

Code provided below

type or paste code here/*----------------------------------------------------------------------------*/
/*                                                                            */
/*    Module:       main.cpp                                                  */
/*    Author:       VEX                                                       */
/*    Created:      Thu Sep 26 2019                                           */
/*    Description:  Competition Template                                      */
/*                                                                            */
/*----------------------------------------------------------------------------*/

// ---- START VEXCODE CONFIGURED DEVICES ----
// Robot Configuration:
// [Name]               [Type]        [Port(s)]
// Vision10             vision        10              
// Optical9             optical       9               
// Distance11           distance      11              
// Distance20           distance      20              
// Optical19            optical       19              
// Controller1          controller                    
// Drivetrain           drivetrain    17, 18          
// Motor16              motor         16              
// ---- END VEXCODE CONFIGURED DEVICES ----

#define _USE_MATH_DEFINES
 
#include <cmath>
#include <math.h>
#include <algorithm>
#include <iostream>
#include "vex.h"

using namespace vex;

// A global instance of competition
competition Competition;

// define your global instances of motors and other devices here

\
motor lF = motor(PORT6, ratio6_1, true);
motor lM = motor(PORT5, ratio6_1,true);

motor rF = motor(PORT3, ratio6_1, false);
motor rM = motor(PORT2, ratio6_1,false);

motor_group lefts = motor_group(lF,lM);
motor_group rights = motor_group(rF,rM);
drivetrain all = drivetrain(lefts,rights);






/*---------------------------------------------------------------------------*/
/*                          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() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

}

/*--------------------------------''/-------------------------------------------*/
/*                                                                           */
/*                              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 closer(void)
  {

      
  }

void far(void)
  {
    
      
  }

void skills(void)
  {
    
  }
void autonomous(void) 
{

  
}

/*---------------------------------------------------------------------------*/
/*                                                                           */
/*                              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) 
  {
    int y = Controller1.Axis3.position(percent);
    int x = Controller1.Axis1.position(percent);
    
    int neg = 9998 - Distance20.objectDistance(mm);
    if( Distance20.objectDistance(mm) < 0)
      {
        neg = 0;
       }


        Controller1.Screen.print((Distance20.objectDistance(mm)) );
        Controller1.Screen.print("  " );
        Controller1.Screen.print(neg );
        Controller1.Screen.newLine();
        lefts.setVelocity((y+x)/neg , percent);
        rights.setVelocity((y-x)/neg, percent);
        
        wait(1,msec);
        



    lefts.setVelocity((y+x)/neg , percent);
    rights.setVelocity((y-x)/neg, percent);

    rights.spin(forward);
    lefts.spin(forward);
    
  }
}

//
// 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);

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

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

.
Thank you for any help or advice!

What are some regular values that are printed for neg on the controller screen? From what I see you are dividing a value that at most is 200 (y+x) by something in the thousand which would cause the speed value to round down to 0 since it takes an int.

Nvm it takes a double but the value would still be bellow 0

would the code mess up if neg was equal to 0? (divide by 0 errors)

now it makes sense, the neg value is no large that it round to zero, so using a higher magnitude measurement would be better lime cm, dm, or m.

You could try making it into a PID instead of just P and just subtracting. You probably should add a little buffer to prevent it from overshooting into the wall

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.