Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

ESP32 ‐ WROOM

Matheus Cirillo edited this page Dec 1, 2024 · 3 revisions

Unpacking Communication

This section focuses on receiving and processing commands over the I2C interface. The commands are received as strings, parsed, and converted into structured data for further processing.

Each command follows the structure:

<NUMBER> <NUMBER> <NUMBER>\n
  • <NUMBER>: An integer (can be negative).
  • Spaces: Separators between numbers.
  • \n: Newline character marking the end of a command.

Where everything is processed inside the main:

Data Reception:

The i2c_slave.read_buffer() function reads data from the I2C bus. If no data is received (received_data == NULL), the loop continues without processing.

Command Division:

Because commands can accumulate in the buffer divideI2CPacket(received_data, &command_count) splits the received data into individual commands based on the newline character \n. It returns an array of command strings (commands) and updates command_count with the number of valid commands.

Command Parsing:

Iterates over each command string. Uses parseServoCommand(command) to convert the string into a RpiDataPacket structure containing the command and its parameters. If parsing fails (numbers == NULL), it skips to the next command.

Command Processing:

After parsing the commands into RpiDataPacket structures, the system uses a switch statement to handle different command types based on the command field of the RpiDataPacket.

1. Set Angle (SET_ANGLE)

  • Purpose: Adjusts the angle of a specified servo.
  • Validation:
    • Checks if the servo ID (param1) is valid.
    • Ensures that auto-aim is not enabled, as manual control is only allowed when auto-aim is disabled.
  • Action:
    • Calls the set_angle method on the specified servo with the desired angle (param2).
    • Resets both the X and Y PID controllers to prevent any residual control effects from previous operations.

2. Set Minimum Pulse Width (SET_MIN_PULSE_WIDTH)

  • Purpose: Updates the minimum pulse width setting for a specified servo.
  • Validation:
    • Checks for a valid servo ID.
    • Ensures auto-aim is disabled to allow manual configuration.
  • Action:
    • Calls set_min_pulse_width on the specified servo with the new minimum pulse width value.

3. Set Maximum Pulse Width (SET_MAX_PULSE_WIDTH)

  • Purpose: Updates the maximum pulse width setting for a specified servo.
  • Validation:
    • Checks for a valid servo ID.
    • Ensures auto-aim is disabled.
  • Action:
    • Calls set_max_pulse_width on the specified servo with the new maximum pulse width value.

4. Set Error (SET_ERROR)

  • Purpose: Adjusts servo positions based on error values from a tracking system, primarily used when auto-aim is enabled.
  • Validation:
    • Ensures that auto-aim is not in manual mode; the command is ignored if it is.
  • Process:
    • Creates an error coordinate with x and y values from param1 and param2.
    • If both error values are zero:
      • Checks if a "reseter" task is already running.
      • If not, creates a "reseter" task to reset the controllers after a delay.
      • Skips further processing to avoid unnecessary calculations.
    • If the error is non-zero and a "reseter" task exists:
      • Deletes the "reseter" task to prevent it from resetting the controllers prematurely.
  • Action:
    • Calculates control outputs using the X and Y PID controllers based on the error values.
    • Logs the error and control values for debugging purposes.
    • Adjusts the servos by adding the control outputs to their current pulse widths using the add_pulse_width method.

5. Set Controller Parameters (SET_CONTROLLER_PARAMETERS)

  • Purpose: Updates specific parameters of the PID controllers or resets them.
  • Process:
    • Casts param1 to a ControllerParameters enum to identify which parameter to update.
    • Converts param2 to a floating-point value (dividing by 1000.0) for precision.
  • Action:
    • Depending on the parameter specified (KP, KI, KD, KS, or RESET), it updates the corresponding gains of both X and Y PID controllers or resets them.
  • Error Handling:
    • Logs an error message if an invalid controller parameter is specified.

6. Toggle Auto-Aim (TOGGLE_AUTO_AIM)

  • Purpose: Changes the state of the auto-aim feature.
  • Validation:
    • Checks if the new auto-aim state (param2) is within the valid range.
  • Action:
    • Updates the auto_aim_state variable by casting param2 to the AutoAimState enum.
    • Logs the new auto-aim state for confirmation.

7. Default Case

  • Purpose: Handles any unrecognized or invalid commands.
  • Action:
    • Logs an error message indicating that an invalid command was received.

Clone this wiki locally

AltStyle によって変換されたページ (->オリジナル) /