Control Software

Overview of the control software for Drosophibot II

Code is reproduced here, but can also be found at this public GitHub repo:

Downloading from GitHub is advised for actually running the robot, to ensure you have the most recent version.


Presently, control signals are provided by an external laptop through a U2D2 serial converter (Robotis, Seoul, South Korea) connected to the power board. The laptop runs a MATLAB script that writes servo angle commands formulated by the kinematic solver to each servo. The script then reads the present servo angles and the current draw of each servo. The MATLAB script additionally communicates over Serial communication with the OpenCM microcontroller on the robot running an Arduino (Arduino, Monza, Italy) script that collects and broadcasts strain data from the 36 strain gauge locations. The read-write loop runs between 35-40 Hz.

The following sections will describe each component of this controller architecture in greater detail.

Arduino Script

chevron-rightFull Arduino scripthashtag

The Arduino script runs on the robot's OpenCM microcontroller and facilitates the collection of the strain data by calibrating the gauges at the start of the trial, organizing the switching of the deMUX board during data collection, and sending the strain data over Serial communication to the MATLAB program.

circle-info

If strain data is not being collected for a trial, using this script is unnecessary and can be omitted.

Upon startup, the program sets all the digital pins on the microcontroller LOW and specifies them as outputs. It similarly specifies all of the analog pins as inputs, then waits for the MATLAB program to send a command over Serial to continue the program. Once this command is received, the program calibrates the rest point of all 36 strain gauges (lines 62-102) following the following process:

  1. The digital pins for a particular leg and side of the amplifier board are set HIGH to direct the I2C connections to the desired strain circuit. The digital pin for a particular strain location is also set HIGH to connect that strain circuit to that leg's analog pin.

  2. A strain reading is collected from that location and compared to a user-defined target rest value.

  3. IF the strain is not within a user-defined threshold of the target, THEN the wiper value of the circuit's digipot is changed.

    1. IF the value is less than the lower calibration limit, THEN the wiper value is incremented.

    2. IF the value is greater than the upper calibration limit, THEN the wiper value is decremented

  4. The strain is rerecorded to check the wiper value's effect on the reading.

This process is repeated until the strain reading is within the calibration range, then repeated for all strain gauge locations across all legs.

Once calibration is complete, the program enters the main strain reading loop (lines 104-138). The loop requires the MATLAB program to send a command variable requesting strain data to initiate strain reading. Otherwise, the loop does nothing. When active, the loop continuously reads strain data from each location on each leg. It accomplishes this by cycling through setting one of the six digital command pins HIGH, thereby connecting a particular location to its leg's analog pin, then reading from all six legs' pins. The program then sends the combined data from all 36 locations (allSGValues on line 128) as one line over Serial to the MATLAB program before waiting for 2 ms and continuing with another set of strain readings. In this way, the Arduino program facilitates sending calibrated strain data to the MATLAB program when requested.

MATLAB Script

chevron-rightFull MATLAB scripthashtag

The current function of the MATLAB script is to control the robot using feedforward kinematic replay from the solver, then collect and record sensory data from the servos and the strain gauges. It does so by first importing DoF angle and torque data from the output of an instance of the solver defined by the user (lines 87-96). This data is then preprocessed before the trial begins (lines 100-189). To account for differences in component rigidity between the solver and the physical platform (e.g., gear backlash, 3-D printed material elasticity, foot slippage) causing unwanted deflections and sagging, a bias term is added to the solver's angle commands equal to the solver's expected actuator torque divided by the stiffness coefficient of the actuator (i.e., the proportional feedback gain scaled to Nm/rad, kP; line 127). These changes bring the posture of the robot closer to the planned posture in the solver. The biased DoF angle data is then converted from radians to bits to be suitable for sending as actuator commands (lines 131-139). Once the angle data is biased and converted, the program determines at what timestep all legs are in stance (lines 114-123) and shifts the data by that amount (149-150). This shift ensures that all legs begin on the ground for each walking trial. Finally, the data is down-sampled to ensure the correct amount of points for the robot's read-write frequency (lines 144-146). Based on the user-defined number of steps for the trial and the length of the downsampled step data, a numIters variable is defined as the total number of timesteps for the trial (line 152).

Once this pre-processing is completed, the robot control portion of the program begins. Connection to each of the robot's actuators is established and the desired internal parameters (maximum velocity, KP value, etc.) are set in each servomotor. Each actuator is then commanded to the first position in the walking command data. If data is to be recorded from the strain gauges, the program sends a signal over Serial to the OpenCM to start sensor calibration following a button-press. Once it receives a completion indicator from the OpenCM, another button-press has the program command the OpenCM to stream 20 iteration's worth of strain data. This data is later averaged to find the baseline value of each gauge and normalize the strain data post hoc. One last button-press halts the Arduino streaming over Serial and begins the stepping control loop.

The stepping control loop cycles numIters times, each time first commanding the servos to the appropriate angle in the step cycle. The code then continuously reads the actual position and load until the loop time reaches the designated duration actTimeStep specified during the downsampling in order to give the servos time to move. If strain is being recorded, the strain data is additionally commanded from the OpenCM and recorded during this waiting period. Once the loop has executed numIters times, the robot stops walking and the trial ends. A button-press disables the torques for each servo, and the code post-processes the data before saving it to a user-defined location. This post-processing primarily entails converting the angles and loads from bit values into radian and Newton-meter values, respectively, as well as parsing the strings of strain data into a matrix form. The output from each trial is a .mat file with the raw and post-processed data, as well as a text file documenting the details of the trial (number of steps, terrain, step period, etc.).

Last updated

Was this helpful?