Testprogram for TMC2209-Library. Uses Speed-Control via VACTUAL instead of Step/Dir
Dependencies: TMCStepper mRotaryEncoder-os
main.cpp@3:209a9c414f54, 2021-03-16 (annotated)
- Committer:
- charly
- Date:
- Tue Mar 16 21:13:37 2021 +0000
- Revision:
- 3:209a9c414f54
- Parent:
- 2:94c5b3f09463
- Child:
- 4:12bfa2c1729f
Basic Test-Version
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 | 2:94c5b3f09463 | 4 | |
charly | 2:94c5b3f09463 | 5 | /* |
charly | 2:94c5b3f09463 | 6 | Testprogram for TMCStepper-Library |
charly | 2:94c5b3f09463 | 7 | TMCStepper based on https://github.com/teemuatlut/TMCStepper for Arduino |
charly | 2:94c5b3f09463 | 8 | by https://github.com/teemuatlut |
charly | 2:94c5b3f09463 | 9 | +++++ |
charly | 2:94c5b3f09463 | 10 | Tested with https://github.com/bigtreetech/BIGTREETECH-TMC2209-V1.2 |
charly | 2:94c5b3f09463 | 11 | */ |
charly | 0:3f4cfbeda9d3 | 12 | |
charly | 0:3f4cfbeda9d3 | 13 | |
charly | 0:3f4cfbeda9d3 | 14 | DigitalOut ledDir(LED2); |
charly | 0:3f4cfbeda9d3 | 15 | //Virtual serial port over USB with 15200 baud 8N1 |
charly | 0:3f4cfbeda9d3 | 16 | static BufferedSerial host(USBTX, USBRX,115200); |
charly | 0:3f4cfbeda9d3 | 17 | |
charly | 2:94c5b3f09463 | 18 | // hardware parameters: |
charly | 0:3f4cfbeda9d3 | 19 | // MOTOR Steps per Revolution ( 1/8 Microsteps, 200Steps per Rev / 1.8 degrees per FullStep) |
charly | 0:3f4cfbeda9d3 | 20 | #define MSPR 1600 |
charly | 0:3f4cfbeda9d3 | 21 | // Gear Ratio |
charly | 0:3f4cfbeda9d3 | 22 | #define GR 288 |
charly | 0:3f4cfbeda9d3 | 23 | |
charly | 0:3f4cfbeda9d3 | 24 | #define DRIVER_ADDRESS 0b00 // TMC2209 Driver address according to MS1 and MS2 |
charly | 3:209a9c414f54 | 25 | #define R_SENSE 0.11f // R-Sense in OHM.Match to your driver |
charly | 2:94c5b3f09463 | 26 | #define MICROSTEPS 128 // # of microsteps |
charly | 2:94c5b3f09463 | 27 | #define RMSCURRENT 500 // RMS current of Stepper Coil in mA |
charly | 0:3f4cfbeda9d3 | 28 | |
charly | 2:94c5b3f09463 | 29 | // A TMCStepper-object with UART and given address and R-Sense |
charly | 0:3f4cfbeda9d3 | 30 | //RX, TX, RS, Addr |
charly | 0:3f4cfbeda9d3 | 31 | TMC2209Stepper stepper(p14, p13, R_SENSE, DRIVER_ADDRESS); |
charly | 0:3f4cfbeda9d3 | 32 | |
charly | 0:3f4cfbeda9d3 | 33 | // Assumes little endian |
charly | 0:3f4cfbeda9d3 | 34 | void printBits(size_t const size, void const * const ptr) |
charly | 0:3f4cfbeda9d3 | 35 | { |
charly | 0:3f4cfbeda9d3 | 36 | unsigned char *b = (unsigned char*) ptr; |
charly | 0:3f4cfbeda9d3 | 37 | unsigned char byte; |
charly | 0:3f4cfbeda9d3 | 38 | int i, j; |
charly | 0:3f4cfbeda9d3 | 39 | // puts("#"); |
charly | 0:3f4cfbeda9d3 | 40 | for (i = size-1; i >= 0; i--) { |
charly | 0:3f4cfbeda9d3 | 41 | for (j = 7; j >= 0; j--) { |
charly | 0:3f4cfbeda9d3 | 42 | byte = (b[i] >> j) & 1; |
charly | 0:3f4cfbeda9d3 | 43 | printf("%u", byte); |
charly | 0:3f4cfbeda9d3 | 44 | } |
charly | 0:3f4cfbeda9d3 | 45 | } |
charly | 0:3f4cfbeda9d3 | 46 | // puts("#"); |
charly | 0:3f4cfbeda9d3 | 47 | } |
charly | 0:3f4cfbeda9d3 | 48 | |
charly | 0:3f4cfbeda9d3 | 49 | int main() |
charly | 0:3f4cfbeda9d3 | 50 | { |
charly | 0:3f4cfbeda9d3 | 51 | printf("\r\nConnected to mbed\r\n"); |
charly | 2:94c5b3f09463 | 52 | stepper.begin(); // UART: Init SW UART (if selected) with default baudrate |
charly | 2:94c5b3f09463 | 53 | stepper.toff(3); // Enables driver in software - 3, 5 ???? |
charly | 2:94c5b3f09463 | 54 | stepper.rms_current(RMSCURRENT); // Set motor RMS current in mA / min 500 for 24V/speed:3000 |
charly | 1:60419aa0c030 | 55 | // 1110, 800 |
charly | 0:3f4cfbeda9d3 | 56 | stepper.microsteps(MICROSTEPS); // Set microsteps to 1:Fullstep ... 256: 1/256th |
charly | 1:60419aa0c030 | 57 | stepper.en_spreadCycle(true); // Toggle spreadCycle on TMC2208/2209/2224: default false, true: much faster!!!! |
charly | 0:3f4cfbeda9d3 | 58 | stepper.pwm_autoscale(true); // Needed for stealthChop |
charly | 0:3f4cfbeda9d3 | 59 | printf("TMC-Version: %02X\r\n",stepper.version()); |
charly | 0:3f4cfbeda9d3 | 60 | |
charly | 1:60419aa0c030 | 61 | bool shaft = false; //direction CW or CCW |
charly | 1:60419aa0c030 | 62 | |
charly | 0:3f4cfbeda9d3 | 63 | while(1) { |
charly | 0:3f4cfbeda9d3 | 64 | // printf("TSTEP(): %i\r\n", stepper.TSTEP()); |
charly | 0:3f4cfbeda9d3 | 65 | uint32_t status = stepper.DRV_STATUS(); |
charly | 0:3f4cfbeda9d3 | 66 | printf("DRV_STATUS(): "); printBits(sizeof(status),&status);printf("\r\n"); |
charly | 0:3f4cfbeda9d3 | 67 | uint32_t ioin = stepper.IOIN(); |
charly | 0:3f4cfbeda9d3 | 68 | printf("IOIN(): "); printBits(sizeof(ioin),&ioin);printf("\r\n"); |
charly | 0:3f4cfbeda9d3 | 69 | // uint32_t otp = stepper.OTP_READ(); |
charly | 0:3f4cfbeda9d3 | 70 | // printf("OTP_READ(): ");printBits(sizeof(otp),&otp);printf("\r\n"); |
charly | 0:3f4cfbeda9d3 | 71 | |
charly | 1:60419aa0c030 | 72 | printf("VACTUAL(): %zu \r\n", stepper.VACTUAL()); |
charly | 0:3f4cfbeda9d3 | 73 | // increase |
charly | 1:60419aa0c030 | 74 | uint32_t maxspeed = 3000; //max 3400 or 3000 |
charly | 1:60419aa0c030 | 75 | uint32_t actspeed = 0; |
charly | 0:3f4cfbeda9d3 | 76 | while (actspeed < maxspeed) { |
charly | 0:3f4cfbeda9d3 | 77 | actspeed += 200; |
charly | 1:60419aa0c030 | 78 | if (actspeed > maxspeed) { |
charly | 1:60419aa0c030 | 79 | actspeed = maxspeed; |
charly | 1:60419aa0c030 | 80 | } |
charly | 3:209a9c414f54 | 81 | printf("actspeed: %i",actspeed); |
charly | 0:3f4cfbeda9d3 | 82 | stepper.VACTUAL(actspeed*MICROSTEPS);// Set Speed to value |
charly | 1:60419aa0c030 | 83 | ThisThread::sleep_for(25ms); //wait |
charly | 0:3f4cfbeda9d3 | 84 | } |
charly | 0:3f4cfbeda9d3 | 85 | printf("VACTUAL(): %zu \r\n", stepper.VACTUAL()); |
charly | 3:209a9c414f54 | 86 | ThisThread::sleep_for(5s); |
charly | 0:3f4cfbeda9d3 | 87 | // decrease |
charly | 0:3f4cfbeda9d3 | 88 | maxspeed = 0; |
charly | 0:3f4cfbeda9d3 | 89 | while (actspeed > maxspeed) { |
charly | 0:3f4cfbeda9d3 | 90 | actspeed -= 200; |
charly | 1:60419aa0c030 | 91 | if (actspeed < 0) { |
charly | 1:60419aa0c030 | 92 | actspeed = 0; |
charly | 1:60419aa0c030 | 93 | } |
charly | 3:209a9c414f54 | 94 | printf("actspeed: %i",actspeed); |
charly | 0:3f4cfbeda9d3 | 95 | stepper.VACTUAL(actspeed*MICROSTEPS);// Set Speed to value |
charly | 1:60419aa0c030 | 96 | ThisThread::sleep_for(25ms); //wait |
charly | 0:3f4cfbeda9d3 | 97 | } |
charly | 1:60419aa0c030 | 98 | |
charly | 1:60419aa0c030 | 99 | // stepper.VACTUAL(400*MICROSTEPS);// Set Speed to value |
charly | 0:3f4cfbeda9d3 | 100 | ThisThread::sleep_for(5s); //wait |
charly | 1:60419aa0c030 | 101 | // inverse direction |
charly | 1:60419aa0c030 | 102 | shaft = !shaft; |
charly | 1:60419aa0c030 | 103 | stepper.shaft(shaft); |
charly | 2:94c5b3f09463 | 104 | // Read Interace-Count |
charly | 2:94c5b3f09463 | 105 | printf("IFCNT(): %zu \r\n",stepper.IFCNT()); |
charly | 1:60419aa0c030 | 106 | printf("...\r\n"); |
charly | 0:3f4cfbeda9d3 | 107 | } |
charly | 0:3f4cfbeda9d3 | 108 | } |