Sine wave generator

In this section:
Sine wave generator →
Make the R3PTAR slither →

Sine wave generator

In this section, the C# code for a simple sine wave generator is described. The main purpose is to serve as an oscillating reference to the Ev3 Medium Motor on board the R3PTAR, so that the snake neck can oscillate accordingly. The following equation describes a sine wave that varies with time:

y(t) = A_0 + A\sin{(2\pi f t)}, where

  • A_0 is the mean value of the wave,
  • A is the wave amplitude,
  • f is the frequency expressed in Hz and,
  • t is time expressed in seconds.

On our robots, or on board every digital device for what matters, time cannot vary continuously and so we have to discretize the time by posing t=kT, where k is the independent discrete time, and T is the sampling time expressed in seconds; so we get:

y(kT) = A_0 + A\sin{(2\pi f k T)}, which sometimes is written as

y_k = A_0 + A\sin{(2\pi f k T)}.

The following thread body contains the code that generates the sine wave given its mean value, frequency and sample time;

The code is pretty straightforward; just take note of the  if statement that reverts the k variable to zero when it grows bigger than the largest representable integer value.

Make the R3PTAR slither

So, making the R3PTAR slither becomes much simpler now: just use a PID controller to regulate the angular position of the Ev3 Medium Motor of the robot, and send it the output of the sine wave generator plus a mean value as a reference. See the following picture for clarity:

This scheme illustrates how to program the R3PTAR to let it move like a snake
This scheme illustrates how to program the R3PTAR to let it move like a snake

The mean value is the key to steer the robot in the desired direction: if the mean value is positive the robot will steer to the right, else to the left… always slithering as a real snake.

To change the behavior of the snake according to your preference, you can do the following:

  • increase or decrease the amplitude of the sine wave if you want a wider or a tighter undulation;
  • increase or decrease the frequency of the sine wave to get a more nervous or a smoother movement.


Sine wave generator for the R3PTAR
Tagged on: