PID regulated SOLEX moped

Hello friends! First of all, sorry for my english… Doing my best!

I’m going to convert an old SOLEX moped (with broken engine) to an electric bicycle.

I have a lot of ideas on fun things I want to add to it were the Arduino is going to be involved:
PID-cruise control. (for the fun of it) I think it’s awesome.
GPS (later project)
Lights (easy done)
Speedometer (easy done)
“Battery health meter” (easy done)

Now first of all I’ve looked trough this excellent PID library by Brett Beauregard.
I put together some code from one of Bretts example code and a bit of code that read revolutions/sec from the motor with a hall effect switch.

I plugged everything in and it kinda worked (the motor acted kinda strange but run smoothly). First of all I don’t know how to tune the PID and I’m not sure the code do what I think it should.

This is my idea of how it works:
Input: rev/sec from the hall effect switch.
Setpoint: The speed I set with a potentiometer.
Output: PWM from Arduino to my motor driver.

/********************************************************
 * PID Simple Example
 * Reading hz to control analog PWM output 3
 ********************************************************/

#include <PID_Beta6.h>

//+++++FEEDBACK
 volatile byte hzcount;

 unsigned long hz;

 unsigned long timeold;
//+++++FEEDBACK

//Define Variables we'll be connecting to
double Setpoint, Input, Output;

//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint,2,5,1);

void setup()
{
  //+FEEDBACK+ from hall effect switch
 attachInterrupt(0, hz_fun, RISING);

   hzcount = 0;
   hz = 0;                       // [b]Input[/b]
   timeold = 0;
 //+FEEDBACK+ from hall effect switch
 
  //initialize the variables we're linked to
  Input = hz;
  Setpoint = (analogRead(0)/9)+140;

  //turn the PID on
  myPID.SetMode(AUTO);
  Serial.begin(9600);
}

void loop()
{
  
  //+FEEDBACK+ from hall effect switch
if (hzcount >= 5) { //higher number for better resolution
     hz = 1000/(millis() - timeold)*hzcount+140; //140->206 //motor rev/sec is max 66
     timeold = millis();
     hzcount = 0;
}   
 //+FEEDBACK+ from hall effect switch
 
  Input = hz;
  myPID.Compute();
  analogWrite(3,Output); // [b]PWM to motor controller[/b]
}

 //+FEEDBACK+ from hall effect switch
 void hz_fun()
 {
   hzcount++;
 }
 //+FEEDBACK+ from hall effect switch

You may ask what things like +140 is for:
My motor controller is a CURTIS with wig-wag control. It can be controlled with a 0-5V output from the Arduino (with a RC-filter between). It has a neutral mode if the voltage is between 2.2V and 2.8V. If it’s more than 2.8V the motor spins in one direction and if it’s less than 2.2V it spins in the other direction.
So the output should never go under AnalogWrite 140 (I’ve tested the CURTIS with different PWM and 140 seems like a good value for neutral.)
The motor goes up to 66rev/sec. (BOSCH GPA 24V 750W)

So do I need to add any parameters to the PID tuning of the code and what parameters for P I and D should I start workingfrom. Guess it’s something else with cruise control than for example controlling heat.



Best regards
//Isak Nordell SWEDEN