pioneers / runtime

Firmware for the PiE kit robots and devices
7 stars 2 forks source link

[EXECUTOR] Various refactoring #266

Open benliao1 opened 1 year ago

benliao1 commented 1 year ago

In order to make the code easier for students to write and simplify the executor logic on our end, we've decided to try to change the way student code works to simply have one function that runs once in autonomous and another function that runs once in teleop. The student API functionality should be unchanged.

Essentially, sample student code will be like:

MOTOR_ID = "6_1234"

def autonomous():
    Robot.set_value(MOTOR_ID, "velocity_a", 1.0)
    Robot.set_value(MOTOR_ID, "velocity_b", 1.0)
    Robot.sleep(1)
    Robot.set_value(MOTOR_ID, "velocity_a", 0.0)
    Robot.set_value(MOTOR_ID, "velocity_b", 0.0)

def teleop():
    while True:
        left_stick = Gamepad.get_value("joystick_left_y")
        right_stick = Gamepad.get_value("joystick_right_y")
        if abs(left_stick) > 0.05:
            Robot.set_value(MOTOR_ID, "velocity_a", left_stick)
        else:
            Robot.set_value(MOTOR_ID, "velocity_a", 0.0)

        if abs(right_stick) > 0.05:
            Robot.set_value(MOTOR_ID, "velocity_b", right_stick)
        else:
            Robot.set_value(MOTOR_ID, "velocity_b", 0.0)        

Notice the lack of a setup function and the requirement of a while True loop around the entire teleop function. This will hopefully make more intuitive sense to the students and make it easier for us to write executor (we don't need to be calling Python functions from a C program continuously.

Additionally, it would be useful to look into whether we can remove Cython as a dependency completely from Runtime. Since Cython is such a massive library / tool, it will be really difficult for new Runtime members to pick it up and make meaningful changes to executor in the future if it is kept in Runtime (no one has learned in the three years since Ashwin wrote it in summer 2020). So, if it is at all possible, we should try to remove Cython as a dependency of Runtime.

AVSurfer123 commented 1 year ago

Hey it's been a while and was perusing my Github notifications.

I still think Cython is relatively simple to learn when compared to the Python C interface (https://docs.python.org/3/extending/extending.html#). But I guess it does eliminate one library since executor.c is already using this interface regardless. It will just be pretty complex to rewrite studentapi.pyx, and you'll need to be wary about Python reference counting + figuring out threads for actions.