Testprogram for TMC2209-Library. Uses Speed-Control via VACTUAL instead of Step/Dir
Dependencies: TMCStepper mRotaryEncoder-os
main.cpp@4:12bfa2c1729f, 2021-03-18 (annotated)
- Committer:
- charly
- Date:
- Thu Mar 18 20:48:31 2021 +0000
- Revision:
- 4:12bfa2c1729f
- Parent:
- 3:209a9c414f54
- Child:
- 5:7f250f463aa2
working version with rotary encoder for speed-selection.; No speedlimit!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
charly | 0:3f4cfbeda9d3 | 1 | #include "mbed.h" |
charly | 0:3f4cfbeda9d3 | 2 | #include "platform/mbed_thread.h" |
charly | 2:94c5b3f09463 | 3 | #include "TMCStepper.h" |
charly | 4:12bfa2c1729f | 4 | #include "mRotaryEncoder.h" |
charly | 2:94c5b3f09463 | 5 | |
charly | 2:94c5b3f09463 | 6 | /* |
charly | 2:94c5b3f09463 | 7 | Testprogram for TMCStepper-Library |
charly | 2:94c5b3f09463 | 8 | TMCStepper based on https://github.com/teemuatlut/TMCStepper for Arduino |
charly | 2:94c5b3f09463 | 9 | by https://github.com/teemuatlut |
charly | 2:94c5b3f09463 | 10 | +++++ |
charly | 2:94c5b3f09463 | 11 | Tested with https://github.com/bigtreetech/BIGTREETECH-TMC2209-V1.2 |
charly | 2:94c5b3f09463 | 12 | */ |
charly | 0:3f4cfbeda9d3 | 13 | |
charly | 0:3f4cfbeda9d3 | 14 | |
charly | 0:3f4cfbeda9d3 | 15 | DigitalOut ledDir(LED2); |
charly | 0:3f4cfbeda9d3 | 16 | //Virtual serial port over USB with 15200 baud 8N1 |
charly | 0:3f4cfbeda9d3 | 17 | static BufferedSerial host(USBTX, USBRX,115200); |
charly | 0:3f4cfbeda9d3 | 18 | |
charly | 4:12bfa2c1729f | 19 | //mRotaryEncoder(PinName pinA, PinName pinB, PinName pinSW, PinMode pullMode=PullUp, int debounceTime_us=1000) |
charly | 4:12bfa2c1729f | 20 | mRotaryEncoder wheel(p16, p17, p18,PullUp,1500); |
charly | 4:12bfa2c1729f | 21 | |
charly | 2:94c5b3f09463 | 22 | // hardware parameters: |
charly | 0:3f4cfbeda9d3 | 23 | // MOTOR Steps per Revolution ( 1/8 Microsteps, 200Steps per Rev / 1.8 degrees per FullStep) |
charly | 0:3f4cfbeda9d3 | 24 | #define MSPR 1600 |
charly | 0:3f4cfbeda9d3 | 25 | // Gear Ratio |
charly | 0:3f4cfbeda9d3 | 26 | #define GR 288 |
charly | 0:3f4cfbeda9d3 | 27 | |
charly | 0:3f4cfbeda9d3 | 28 | #define DRIVER_ADDRESS 0b00 // TMC2209 Driver address according to MS1 and MS2 |
charly | 3:209a9c414f54 | 29 | #define R_SENSE 0.11f // R-Sense in OHM.Match to your driver |
charly | 2:94c5b3f09463 | 30 | #define MICROSTEPS 128 // # of microsteps |
charly | 4:12bfa2c1729f | 31 | #define RMSCURRENT 600 // RMS current of Stepper Coil in mA |
charly | 0:3f4cfbeda9d3 | 32 | |
charly | 2:94c5b3f09463 | 33 | // A TMCStepper-object with UART and given address and R-Sense |
charly | 0:3f4cfbeda9d3 | 34 | //RX, TX, RS, Addr |
charly | 0:3f4cfbeda9d3 | 35 | TMC2209Stepper stepper(p14, p13, R_SENSE, DRIVER_ADDRESS); |
charly | 0:3f4cfbeda9d3 | 36 | |
charly | 4:12bfa2c1729f | 37 | bool enc_pressed = false; // Button of rotaryencoder was pressed |
charly | 4:12bfa2c1729f | 38 | bool enc_rotated = false; // rotary encoder was totaded left or right |
charly | 4:12bfa2c1729f | 39 | int lastGet; |
charly | 4:12bfa2c1729f | 40 | int thisGet; |
charly | 4:12bfa2c1729f | 41 | |
charly | 4:12bfa2c1729f | 42 | //interrup-Handler for button on rotary-encoder |
charly | 4:12bfa2c1729f | 43 | void trigger_sw() { |
charly | 4:12bfa2c1729f | 44 | enc_pressed = true; // just set the flag, rest is done outside isr |
charly | 4:12bfa2c1729f | 45 | } |
charly | 4:12bfa2c1729f | 46 | |
charly | 4:12bfa2c1729f | 47 | //interrup-Handler for rotary-encoder rotation |
charly | 4:12bfa2c1729f | 48 | void trigger_rotated() { |
charly | 4:12bfa2c1729f | 49 | enc_rotated = true; // just set the flag, rest is done outside isr |
charly | 4:12bfa2c1729f | 50 | } |
charly | 4:12bfa2c1729f | 51 | |
charly | 0:3f4cfbeda9d3 | 52 | // Assumes little endian |
charly | 0:3f4cfbeda9d3 | 53 | void printBits(size_t const size, void const * const ptr) |
charly | 0:3f4cfbeda9d3 | 54 | { |
charly | 0:3f4cfbeda9d3 | 55 | unsigned char *b = (unsigned char*) ptr; |
charly | 0:3f4cfbeda9d3 | 56 | unsigned char byte; |
charly | 0:3f4cfbeda9d3 | 57 | int i, j; |
charly | 0:3f4cfbeda9d3 | 58 | // puts("#"); |
charly | 0:3f4cfbeda9d3 | 59 | for (i = size-1; i >= 0; i--) { |
charly | 0:3f4cfbeda9d3 | 60 | for (j = 7; j >= 0; j--) { |
charly | 0:3f4cfbeda9d3 | 61 | byte = (b[i] >> j) & 1; |
charly | 0:3f4cfbeda9d3 | 62 | printf("%u", byte); |
charly | 0:3f4cfbeda9d3 | 63 | } |
charly | 0:3f4cfbeda9d3 | 64 | } |
charly | 0:3f4cfbeda9d3 | 65 | // puts("#"); |
charly | 0:3f4cfbeda9d3 | 66 | } |
charly | 0:3f4cfbeda9d3 | 67 | |
charly | 0:3f4cfbeda9d3 | 68 | int main() |
charly | 0:3f4cfbeda9d3 | 69 | { |
charly | 0:3f4cfbeda9d3 | 70 | printf("\r\nConnected to mbed\r\n"); |
charly | 4:12bfa2c1729f | 71 | |
charly | 4:12bfa2c1729f | 72 | //Int-Handler for RotaryEncoder |
charly | 4:12bfa2c1729f | 73 | // call trigger_sw() when button of rotary-encoder is pressed |
charly | 4:12bfa2c1729f | 74 | wheel.attachSW(&trigger_sw); |
charly | 4:12bfa2c1729f | 75 | // call trigger_rot() when the shaft is rotaded left or right |
charly | 4:12bfa2c1729f | 76 | wheel.attachROT(&trigger_rotated); |
charly | 4:12bfa2c1729f | 77 | lastGet = 0; |
charly | 4:12bfa2c1729f | 78 | // set encrotated, so startup |
charly | 4:12bfa2c1729f | 79 | enc_rotated = true; |
charly | 4:12bfa2c1729f | 80 | |
charly | 2:94c5b3f09463 | 81 | stepper.begin(); // UART: Init SW UART (if selected) with default baudrate |
charly | 2:94c5b3f09463 | 82 | stepper.toff(3); // Enables driver in software - 3, 5 ???? |
charly | 2:94c5b3f09463 | 83 | stepper.rms_current(RMSCURRENT); // Set motor RMS current in mA / min 500 for 24V/speed:3000 |
charly | 1:60419aa0c030 | 84 | // 1110, 800 |
charly | 0:3f4cfbeda9d3 | 85 | stepper.microsteps(MICROSTEPS); // Set microsteps to 1:Fullstep ... 256: 1/256th |
charly | 1:60419aa0c030 | 86 | stepper.en_spreadCycle(true); // Toggle spreadCycle on TMC2208/2209/2224: default false, true: much faster!!!! |
charly | 0:3f4cfbeda9d3 | 87 | stepper.pwm_autoscale(true); // Needed for stealthChop |
charly | 0:3f4cfbeda9d3 | 88 | printf("TMC-Version: %02X\r\n",stepper.version()); |
charly | 0:3f4cfbeda9d3 | 89 | |
charly | 1:60419aa0c030 | 90 | bool shaft = false; //direction CW or CCW |
charly | 1:60419aa0c030 | 91 | |
charly | 0:3f4cfbeda9d3 | 92 | while(1) { |
charly | 4:12bfa2c1729f | 93 | /* |
charly | 0:3f4cfbeda9d3 | 94 | // printf("TSTEP(): %i\r\n", stepper.TSTEP()); |
charly | 0:3f4cfbeda9d3 | 95 | uint32_t status = stepper.DRV_STATUS(); |
charly | 0:3f4cfbeda9d3 | 96 | printf("DRV_STATUS(): "); printBits(sizeof(status),&status);printf("\r\n"); |
charly | 0:3f4cfbeda9d3 | 97 | uint32_t ioin = stepper.IOIN(); |
charly | 0:3f4cfbeda9d3 | 98 | printf("IOIN(): "); printBits(sizeof(ioin),&ioin);printf("\r\n"); |
charly | 0:3f4cfbeda9d3 | 99 | // uint32_t otp = stepper.OTP_READ(); |
charly | 0:3f4cfbeda9d3 | 100 | // printf("OTP_READ(): ");printBits(sizeof(otp),&otp);printf("\r\n"); |
charly | 0:3f4cfbeda9d3 | 101 | |
charly | 1:60419aa0c030 | 102 | printf("VACTUAL(): %zu \r\n", stepper.VACTUAL()); |
charly | 0:3f4cfbeda9d3 | 103 | // increase |
charly | 1:60419aa0c030 | 104 | uint32_t maxspeed = 3000; //max 3400 or 3000 |
charly | 1:60419aa0c030 | 105 | uint32_t actspeed = 0; |
charly | 0:3f4cfbeda9d3 | 106 | while (actspeed < maxspeed) { |
charly | 0:3f4cfbeda9d3 | 107 | actspeed += 200; |
charly | 1:60419aa0c030 | 108 | if (actspeed > maxspeed) { |
charly | 1:60419aa0c030 | 109 | actspeed = maxspeed; |
charly | 1:60419aa0c030 | 110 | } |
charly | 3:209a9c414f54 | 111 | printf("actspeed: %i",actspeed); |
charly | 0:3f4cfbeda9d3 | 112 | stepper.VACTUAL(actspeed*MICROSTEPS);// Set Speed to value |
charly | 1:60419aa0c030 | 113 | ThisThread::sleep_for(25ms); //wait |
charly | 0:3f4cfbeda9d3 | 114 | } |
charly | 0:3f4cfbeda9d3 | 115 | printf("VACTUAL(): %zu \r\n", stepper.VACTUAL()); |
charly | 3:209a9c414f54 | 116 | ThisThread::sleep_for(5s); |
charly | 0:3f4cfbeda9d3 | 117 | // decrease |
charly | 0:3f4cfbeda9d3 | 118 | maxspeed = 0; |
charly | 0:3f4cfbeda9d3 | 119 | while (actspeed > maxspeed) { |
charly | 0:3f4cfbeda9d3 | 120 | actspeed -= 200; |
charly | 1:60419aa0c030 | 121 | if (actspeed < 0) { |
charly | 1:60419aa0c030 | 122 | actspeed = 0; |
charly | 1:60419aa0c030 | 123 | } |
charly | 3:209a9c414f54 | 124 | printf("actspeed: %i",actspeed); |
charly | 0:3f4cfbeda9d3 | 125 | stepper.VACTUAL(actspeed*MICROSTEPS);// Set Speed to value |
charly | 1:60419aa0c030 | 126 | ThisThread::sleep_for(25ms); //wait |
charly | 0:3f4cfbeda9d3 | 127 | } |
charly | 1:60419aa0c030 | 128 | |
charly | 1:60419aa0c030 | 129 | // stepper.VACTUAL(400*MICROSTEPS);// Set Speed to value |
charly | 0:3f4cfbeda9d3 | 130 | ThisThread::sleep_for(5s); //wait |
charly | 1:60419aa0c030 | 131 | // inverse direction |
charly | 1:60419aa0c030 | 132 | shaft = !shaft; |
charly | 1:60419aa0c030 | 133 | stepper.shaft(shaft); |
charly | 2:94c5b3f09463 | 134 | // Read Interace-Count |
charly | 2:94c5b3f09463 | 135 | printf("IFCNT(): %zu \r\n",stepper.IFCNT()); |
charly | 1:60419aa0c030 | 136 | printf("...\r\n"); |
charly | 4:12bfa2c1729f | 137 | */ |
charly | 4:12bfa2c1729f | 138 | // shaft has been rotated? |
charly | 4:12bfa2c1729f | 139 | if (enc_rotated) { |
charly | 4:12bfa2c1729f | 140 | enc_rotated = false; |
charly | 4:12bfa2c1729f | 141 | thisGet = wheel.Get(); |
charly | 4:12bfa2c1729f | 142 | stepper.VACTUAL(thisGet*100*MICROSTEPS);// Set Speed to value |
charly | 4:12bfa2c1729f | 143 | printf("actspeed: %i\r\n",thisGet*100); |
charly | 4:12bfa2c1729f | 144 | |
charly | 4:12bfa2c1729f | 145 | } |
charly | 4:12bfa2c1729f | 146 | // Button pressed? |
charly | 4:12bfa2c1729f | 147 | if (enc_pressed) { |
charly | 4:12bfa2c1729f | 148 | enc_pressed = false; |
charly | 4:12bfa2c1729f | 149 | wheel.Set(0); |
charly | 4:12bfa2c1729f | 150 | thisGet = wheel.Get(); |
charly | 4:12bfa2c1729f | 151 | stepper.VACTUAL(thisGet*100*MICROSTEPS);// Set Speed to value |
charly | 4:12bfa2c1729f | 152 | printf("actspeed: %i\r\n",thisGet*100); |
charly | 4:12bfa2c1729f | 153 | |
charly | 4:12bfa2c1729f | 154 | } |
charly | 4:12bfa2c1729f | 155 | } // while 1 |
charly | 0:3f4cfbeda9d3 | 156 | } |