CPG on Arduino MEGA 2560 using PWM-pins and Soft Robotics Toolkit

Hi everybody!

I hope someone out there is able to help me.

I am working on implementing a central pattern generator (CPG) on an Arduino MEGA 2560 for use to control a pneumatic tentacle. My setup is based on this setup from Soft Robotics Toolkit: Fluidic Control Board | Soft Robotics Toolkit

I have made an SO(2)-network for the CPG which works on my PC, but once I run it on the Arduino, the oscillations are not what I get from my computer.

The plot of the generated oscillations from my computer is attached, as well as the generated oscillations with the same SO(2) parameters on the Arduino. It is clear that I get close to sinusoidal oscillations on my computer, but more of a square waveform on the Arduino.

My code is also based on that of the Soft Robotics Toolkit, though with the SO(2) generating the output for the PWM-pins:

void loop() {

  WeightH1_H1  =  alph*cos(phi);
  WeightH2_H2  =  alph*cos(phi);
  WeightH1_H2  =  alph*sin(phi);
  WeightH2_H1  = -alph*sin(phi);


  BiasH1      = 0.0;
  BiasH2      = 0.0;

  activityH1 = (WeightH1_H1*outputH1+WeightH1_H2*outputH2+BiasH1);
  activityH2 = (WeightH2_H2*outputH2+WeightH2_H1*outputH1+BiasH2);

  outputH1 = (tanh(activityH1));
  outputH2 = (tanh(activityH2));

/*******DELAY LINE*****/

  buffer_cpg.push_back(outputH1);

     //remember the value in last run
    for (int i = 0; i < 2; i++) 
    {
    cpg_pre.at(i) = cpg_motor.at(i);
    }

    //Delay at one motor/valve
    cpg_motor.at(0) = buffer_cpg[buffer_cpg.size() - 2 * 7 - 1];

  /***********************************/

  //*****************//
  potDC1 = 0; potDC2 = 0; potDC3 = 0; potDC4 = 0;

  // if statement for manual switch override
  if (digitalRead(50) == LOW) {potDC1 = analogRead(A1)*100.0/1024.0;} // scale values from pot to 0 to 100, which gets used for duty cycle percentage
 
  if (digitalRead(51) == LOW) { potDC2 = analogRead(A2)*100.0/1024.0; }
  if (digitalRead(52) == LOW) { potDC3 = analogRead(A3)*100.0/1024.0; }
  if (digitalRead(53) == LOW) { potDC4 = analogRead(A4)*100.0/1024.0; }

  float potPWMfq = analogRead(A7)*100.0/1024.0; // scale values from pot to 0 to 100, which gets used for frequency (Hz)
  potPWMfq = 50; //round(potPWMfq/5)*5+1; //50; //1 to 91 Hz in increments of 5 (rounding helps to deal with noisy pot)

  // update PWM output based on the above values from pots
  pPWM(potPWMfq,(outputH1*100.0/1024.0),(outputH1*100.0/1024.0),(cpg_motor.at(0)*100.0/1024.0),potDC4);

The pPWM()-function is defined as:

void pPWM(float pwmfreq, float pwmDC1, float pwmDC2, float pwmDC3, float pwmDC4) {

  // set PWM frequency by adjusting ICR (top of triangle waveform)
  ICR3 = F_CPU / (prescaler * pwmfreq * 2);
  ICR4 = F_CPU / (prescaler * pwmfreq * 2);

  // set duty cycles
  OCR3A = (ICR4) * (pwmDC1 * 0.01);
  OCR4A = (ICR4) * (pwmDC2 * 0.01);
  OCR4B = (ICR4) * (pwmDC3 * 0.01);
  OCR4C = (ICR4) * (pwmDC4 * 0.01);

The values I plot comes from outputH1.
I haven't worked with an Arduino before, so I don't know where the issue lies. Does anybody have any suggestions on what I am doing wrong? I want to acheive the same sinusoidal oscillations as seen on my PC.
Please let me know, if I need to give more information!

Kind regards, Marlene

CPGonArduino.png

CPGonPC.png