Sam Walsh / Mbed 2 deprecated Nucleo_F303K8_DAC_PWM_TEST

Dependencies:   mbed

Fork of Nucleo_FrequencyCounter_Timed by Sam Walsh

Revision:
1:85b1605ed4e0
Parent:
0:fb3e19364d48
Child:
2:29a5b859318a
diff -r fb3e19364d48 -r 85b1605ed4e0 main.cpp
--- a/main.cpp	Tue Jan 26 12:54:37 2016 +0000
+++ b/main.cpp	Wed Feb 24 12:33:26 2016 +0000
@@ -1,37 +1,119 @@
-#include "mbed.h"
+/*
+    Title:       Mbed Motor Controller
+    Author:      Samuel.Walsh@manchester.ac.uk
+    Date:        27/01/16
+    Description: Using School of EEE Motor Driver Board and STM32F303K8 Nucleo32
+    Unipolar PWM Controlling a DC Brushed Motor with PWM on D3, PWM is inverse so
+    1.0 = motor off and 0.0 = motor full
+ */
 
-#define TICKS_PER_REVOLUTION    12.0
-#define TICK_CHECK_PERIOD       0.1
+#include "mbed.h"
+#include "PID.h"
+#include "OneWire_Methods.h"
+#include "ds2781.h"
 
-DigitalOut myled(LED1);
-Serial pc(USBTX, USBRX); // tx, rx
-Ticker periodicTimer1, periodicTimer2;
+#define CURRENT_CHECK_PERIOD_US 50    //Checks ths current of the motor every 100us
+#define TICKS_PER_REVOLUTION    12.0   //The encoder has 12 slots
+#define TICK_CHECK_PERIOD       0.1    //Checks ths speed every 100ms
+#define TICKS_PER_SECOND_MAX    1800.0 //This was found through experimentation with motor, max speed = 8900RPM = ~1800 ticks per second 
+#define TICKS_PER_SECOND_MIN    1*TICKS_PER_REVOLUTION  //This is equal to 1RPS (60RPM) at however many ticks per revolution
+#define SETPOINT_RPM            4000   //Arbitrarily picked
+
+/* Peripherals */
+DigitalOut motorEnable(D8);
+PwmOut motorPWM(D6);
+Ticker periodicTimer1, periodicTimer2, periodicTimer3;
+InterruptIn freqInputPin(D11); 
+AnalogIn motorA_CurrentMinus(A1);
+AnalogIn motorA_CurrentPlus(A0);
+DigitalInOut   one_wire_pin(D9);
+
+//Peripherals used for debugging
+Serial pc(USBTX, USBRX); // tx, rx  just used for debugging
+DigitalOut debuggingLine(D2);
+/* Objects */
+PID controller(0.22, 0.01, 0.001, TICK_CHECK_PERIOD);
  
+/* Motor Variables */
 float speed_rpm, speed_rps, ticks_per_second;
 volatile unsigned int tick_counter, final_count;
+float setPointRPM;
+float MotorACurrent;
 
-InterruptIn freqInputPin(D2); 
+//Onewire battery variables
+int BatteryVoltageReading;
+float BatteryVoltage;
 
 void freqInputPin_Interrupt() {
     //Triggers on the rising edge of the frequency signal
     tick_counter++;
 }
-
 void check_TickCount(){
     final_count = tick_counter;
     //pc.printf("\n\rTick Count is : %d", final_count);
     tick_counter = 0;
+    controller.setProcessValue(final_count);
+    //motorPWM = (float)1.0-controller.compute();
+}
+
+void check_Current(){  
+    MotorACurrent = (3.3f*motorA_CurrentPlus.read()) - (3.3f*motorA_CurrentMinus.read());
 }
 
-void display_TickCount(){
-   //pc.printf("\n\r Final Count is : %.2f", (float) final_count*(1.0/TICK_CHECK_PERIOD));
+void Print_Variables_To_PC(){
+   //BatteryVoltageReading = ReadVoltage();
+   //BatteryVoltage = BatteryVoltageReading*0.00967;
+   
    pc.printf("\n\r Speed in RPM is  : %.2f", (float)(final_count*(1.0/TICK_CHECK_PERIOD))*(60.0/TICKS_PER_REVOLUTION));
+   pc.printf("\n\r Motor Current %.2f", MotorACurrent);
+  // pc.printf("\n\r Battery Voltage %.2f", BatteryVoltage);
 }
 
+
 int main() {
-    freqInputPin.rise(&freqInputPin_Interrupt); //chain interrupt to rising edge
-    periodicTimer1.attach(&check_TickCount, TICK_CHECK_PERIOD);
-    periodicTimer2.attach(&display_TickCount, 1.0); //Display conut once a second
-    while(1) { 
+    //Setup encoder interface
+    freqInputPin.rise(&freqInputPin_Interrupt);                  //chain interrupt to rising edge
+    periodicTimer1.attach(&check_TickCount, TICK_CHECK_PERIOD);  //Check the speed every interval
+    periodicTimer3.attach_us(&check_Current, CURRENT_CHECK_PERIOD_US);  //Check the current every interval see defines above
+    
+    //Setup PID Speed Controller
+    controller.setInputLimits(TICKS_PER_SECOND_MIN, TICKS_PER_SECOND_MAX); //From 60RPM to 8900RPM This needs to be changed for your motor
+    controller.setOutputLimits(0.0, 1.0); //Maximum duty cycle = 1.0, minimum = 0
+    controller.setMode(1);                //1 = auto mode
+    setPointRPM = ((SETPOINT_RPM/60.0)*TICKS_PER_REVOLUTION)/(1.0/TICK_CHECK_PERIOD ); //convert setpoint rpm to ticks per interval
+    controller.setSetPoint(setPointRPM);
+    
+    //Setup PWM
+    motorEnable = 1;
+    motorPWM.period_us(100);  //Set up for 10KHz PWM Switching Frequency
+    
+    //Setup Debugging    
+    periodicTimer2.attach(&Print_Variables_To_PC, 1.0);          //Display stuff on serial port once a second
+    
+    while(1) {
+          motorPWM = 0.5;
+          wait(10);
+          
+          motorPWM = 0.8;
+          wait(5);
+//        setPointRPM = ((1500/60.0)*TICKS_PER_REVOLUTION)/(1.0/TICK_CHECK_PERIOD ); //convert setpoint rpm to ticks per interval
+//        controller.setSetPoint(setPointRPM);
+//        wait(10);
+//        setPointRPM = ((2000/60.0)*TICKS_PER_REVOLUTION)/(1.0/TICK_CHECK_PERIOD ); //convert setpoint rpm to ticks per interval
+//        controller.setSetPoint(setPointRPM);
+//        wait(10);
+//        setPointRPM = ((3000/60.0)*TICKS_PER_REVOLUTION)/(1.0/TICK_CHECK_PERIOD ); //convert setpoint rpm to ticks per interval
+//        controller.setSetPoint(setPointRPM);
+//        wait(10);
+//        setPointRPM = ((500/60.0)*TICKS_PER_REVOLUTION)/(1.0/TICK_CHECK_PERIOD ); //convert setpoint rpm to ticks per interval
+//        controller.setSetPoint(setPointRPM);
+//        wait(10);
+//        setPointRPM = ((2000/60.0)*TICKS_PER_REVOLUTION)/(1.0/TICK_CHECK_PERIOD ); //convert setpoint rpm to ticks per interval
+//        controller.setSetPoint(setPointRPM);
+//        wait(10);
+//        setPointRPM = ((1200/60.0)*TICKS_PER_REVOLUTION)/(1.0/TICK_CHECK_PERIOD ); //convert setpoint rpm to ticks per interval
+//        controller.setSetPoint(setPointRPM);
+//        wait(10);
+
     }
 }