Vladimirtrif / Team-949z_Pelmen_Vex_21-22

0 stars 0 forks source link

Share code across autonomous and opcontrol, have reusable code #10

Open vladsud opened 2 years ago

vladsud commented 2 years ago

The way opcontrol() function is written right now has couple issues:

  1. initialization code (defining all the motors, joystick, etc.) is not shared with autonomous. Any change needs to be remembered to be done in two places, and failure to do so may not be noticed immediately
  2. It's hard to write reusable functions that operate on shared state, like calculate position based on state of all 6 motors. All 6 motor variables would need to be passed into such function, making code too verbose and less readable.

Autonomous class, on the other hand, shows example of how we can improve here. Here is how it looks like:

void autonomous()
{
    Autonomous self_drive;
    self_drive.run();
}

All the interesting code is within Autonomous class. All motors are initialized implicitly when instance of this class is instantiated like shown above.

The direction to improve code sharing would be something like that:

  1. rename Autonomous class to something more generic (Robot class :))
  2. rename run() method on such class to autonomous() method.
  3. Add new method opcontrol() on such class, and move most of the code in today's opcontrol() function into that method. Drop initialization (defining motors) - all of that is already done as part of implicit construction of this class.

After these changes, opcontrol() function will look something like that:


void opcontrol()
{
    Robot robot;
    robot.opcontrol();
}