Testprogram for TMC2209-Library. Uses Speed-Control via VACTUAL instead of Step/Dir

Dependencies:   TMCStepper mRotaryEncoder-os

Files at this revision

API Documentation at this revision

Comitter:
charly
Date:
Thu Mar 18 20:48:31 2021 +0000
Parent:
3:209a9c414f54
Child:
5:7f250f463aa2
Commit message:
working version with rotary encoder for speed-selection.; No speedlimit!

Changed in this revision

mRotaryEncoder-os.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mRotaryEncoder-os.lib	Thu Mar 18 20:48:31 2021 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/charly/code/mRotaryEncoder-os/#d22167ec460c
--- a/main.cpp	Tue Mar 16 21:13:37 2021 +0000
+++ b/main.cpp	Thu Mar 18 20:48:31 2021 +0000
@@ -1,6 +1,7 @@
 #include "mbed.h"
 #include "platform/mbed_thread.h"
 #include "TMCStepper.h"
+#include "mRotaryEncoder.h"
 
 /*
  Testprogram for TMCStepper-Library
@@ -15,6 +16,9 @@
 //Virtual serial port over USB with 15200 baud 8N1
 static BufferedSerial host(USBTX, USBRX,115200);
 
+//mRotaryEncoder(PinName pinA, PinName pinB, PinName pinSW, PinMode pullMode=PullUp, int debounceTime_us=1000)
+mRotaryEncoder  wheel(p16, p17, p18,PullUp,1500);
+
 // hardware parameters:
 // MOTOR Steps per Revolution ( 1/8 Microsteps, 200Steps per Rev / 1.8 degrees per FullStep)
 #define MSPR 1600
@@ -24,12 +28,27 @@
 #define DRIVER_ADDRESS 0b00 // TMC2209 Driver address according to MS1 and MS2
 #define R_SENSE 0.11f   // R-Sense in OHM.Match to your driver
 #define MICROSTEPS 128  // # of microsteps
-#define RMSCURRENT 500  // RMS current of Stepper Coil in mA
+#define RMSCURRENT 600  // RMS current of Stepper Coil in mA
 
 // A TMCStepper-object with UART and given address and R-Sense
 //RX, TX, RS, Addr 
 TMC2209Stepper stepper(p14, p13, R_SENSE, DRIVER_ADDRESS);
 
+bool enc_pressed = false;      // Button of rotaryencoder was pressed
+bool enc_rotated = false;      // rotary encoder was totaded left or right
+int lastGet;
+int thisGet;
+
+//interrup-Handler for button on rotary-encoder
+void trigger_sw() {
+    enc_pressed = true;               // just set the flag, rest is done outside isr
+}
+
+//interrup-Handler for rotary-encoder rotation
+void trigger_rotated() {
+    enc_rotated = true;               // just set the flag, rest is done outside isr
+}
+
 // Assumes little endian
 void printBits(size_t const size, void const * const ptr)
 {
@@ -49,6 +68,16 @@
  int main()
  {
     printf("\r\nConnected to mbed\r\n");
+    
+    //Int-Handler for RotaryEncoder
+    // call trigger_sw() when button of rotary-encoder is pressed
+    wheel.attachSW(&trigger_sw);
+    // call trigger_rot() when the shaft is rotaded left or right
+    wheel.attachROT(&trigger_rotated);
+    lastGet = 0;
+    // set encrotated, so startup
+    enc_rotated = true;
+    
     stepper.begin();                    // UART: Init SW UART (if selected) with default baudrate
     stepper.toff(3);                    // Enables driver in software - 3, 5 ????
     stepper.rms_current(RMSCURRENT);    // Set motor RMS current in mA / min 500 for 24V/speed:3000
@@ -61,6 +90,7 @@
     bool shaft = false;  //direction CW or CCW
     
     while(1) {
+/*        
 //        printf("TSTEP(): %i\r\n", stepper.TSTEP());
         uint32_t status = stepper.DRV_STATUS();
         printf("DRV_STATUS(): "); printBits(sizeof(status),&status);printf("\r\n");
@@ -104,5 +134,23 @@
         // Read Interace-Count
         printf("IFCNT(): %zu \r\n",stepper.IFCNT());
         printf("...\r\n");
-   }
+*/
+    // shaft has been rotated?
+    if (enc_rotated) {
+        enc_rotated = false;
+        thisGet = wheel.Get();
+        stepper.VACTUAL(thisGet*100*MICROSTEPS);// Set Speed to value
+        printf("actspeed: %i\r\n",thisGet*100);
+
+    }
+    // Button pressed?
+    if (enc_pressed) {
+        enc_pressed = false;
+        wheel.Set(0);
+        thisGet = wheel.Get();
+        stepper.VACTUAL(thisGet*100*MICROSTEPS);// Set Speed to value
+        printf("actspeed: %i\r\n",thisGet*100);
+
+    }        
+   } // while 1
  }
\ No newline at end of file
--- a/mbed-os.lib	Tue Mar 16 21:13:37 2021 +0000
+++ b/mbed-os.lib	Thu Mar 18 20:48:31 2021 +0000
@@ -1,1 +1,1 @@
-https://github.com/ARMmbed/mbed-os/#890f0562dc2efb7cf76a5f010b535c2b94bce849
+https://github.com/ARMmbed/mbed-os/#bfde5aa1e74802771eaeacfa74789f71677325cb