-// constants won't change. Used here to 
-// set pin numbers:
+/*
+Author: Baptiste LABAT (Nautilabs)
+Project: robokite
+Date : March 2013
+This file ensures the control of a continuous current motor and gearbox through an H-bridge.
+A Pulse Width Modulation is used. The algorithm uses varying cycle frequency to allow control at low rotation speed.
+Due to minimum quantity of energy to make the motor turn, the motor will not turn smoothly at low speed.
+There is now feedback nor stall control on speed, so the output rotation speed may depend on load torque.
+The motor is controlled through a parameter alpha which is signed and vary from -1 to -1.
+This parameter can be set thanks to a an analog voltage (set through a potentiometer for example) or sent over serial connection
+There is not guarantee that there is a linear relationship between alpha and the rotation speed
+*/
+// \todo this code needs refactorization
+
+// Minimal time to have motor starting at maximum voltage. This will depend on the motor used
+// Try to reduce to get a smooth motion
+double minTime_ms = 30;
+
+// These constants will not change. Used here to 
+// give names to set pin numbers:
 const int HbridgeEnablePin =  13;
 const int HbridgeLogicPin1 = 4;
 const int HbridgeLogicPin2 = 5;
-int sensorValue = 0;        // value read from the potentiometer
-// These constants won't change.  They're used to give names
-// to the pins used:
 const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
 
+int sensorValue = 0;        // Value read from the potentiometer
+
 double alphaSigned = 0;
-String inputString = "";         // a string to hold incoming data
-boolean stringComplete = false;  // whether the string is complete
+String inputString = "";         // A string to hold incoming data
+boolean stringComplete = false;  // Whether the string is complete
 
 void setup()
 {
-  // set the digital pin as output:
+  // Set the digital pin as output:
   pinMode(HbridgeEnablePin, OUTPUT);
   pinMode(HbridgeLogicPin1, OUTPUT);
   pinMode(HbridgeLogicPin2, OUTPUT);
-  // initialize serial communications at 9600 bps:
+  // Initialize serial communications at 9600 bps:
   Serial.begin(9600); 
 
 }
 float StrToFloat(String str){
-  char carray[str.length() + 1]; //determine size of the array
-  str.toCharArray(carray, sizeof(carray)); //put str into an array
+  char carray[str.length() + 1]; // Determine the size of the array
+  str.toCharArray(carray, sizeof(carray)); // Put str into an array
   return atof(carray);
 }
 

 */
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;

//    Serial.println(alphaSigned);   
    alpha = max(fabs(alphaSigned), deadBand);
    alpha = min(alpha, 1);
    if (alphaSigned>fabs(deadBand))
    {
      digitalWrite(HbridgeLogicPin1, HIGH);
      digitalWrite(HbridgeLogicPin2, LOW);
    }
    else 
    {
      if (alphaSigned<-fabs(deadBand))
      {
        digitalWrite(HbridgeLogicPin1, LOW);
        digitalWrite(HbridgeLogicPin2, HIGH);
      }
      else
      {
        digitalWrite(HbridgeLogicPin1, LOW);
        digitalWrite(HbridgeLogicPin2, LOW);
      }
    }
//    Serial.print("alpha = " );                       
//    Serial.println(alpha);   
    elapsedTime_ms = millis() - initialTime;
