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

Dependencies:   TMCStepper mRotaryEncoder-os

Committer:
charly
Date:
Thu Feb 04 21:35:45 2021 +0000
Revision:
1:60419aa0c030
Parent:
0:3f4cfbeda9d3
Child:
2:94c5b3f09463
Fast version with ca. 2,5rpm

Who changed what in which revision?

UserRevisionLine numberNew contents of line
charly 0:3f4cfbeda9d3 1 /* mbed Microcontroller Library
charly 0:3f4cfbeda9d3 2 * Copyright (c) 2019 ARM Limited
charly 0:3f4cfbeda9d3 3 * SPDX-License-Identifier: Apache-2.0
charly 0:3f4cfbeda9d3 4 */
charly 0:3f4cfbeda9d3 5
charly 0:3f4cfbeda9d3 6 #include "mbed.h"
charly 0:3f4cfbeda9d3 7 #include "platform/mbed_thread.h"
charly 0:3f4cfbeda9d3 8
charly 0:3f4cfbeda9d3 9
charly 0:3f4cfbeda9d3 10 DigitalOut ledDir(LED2);
charly 0:3f4cfbeda9d3 11 //Virtual serial port over USB with 15200 baud 8N1
charly 0:3f4cfbeda9d3 12 static BufferedSerial host(USBTX, USBRX,115200);
charly 0:3f4cfbeda9d3 13
charly 0:3f4cfbeda9d3 14
charly 0:3f4cfbeda9d3 15 // apply number of steps, direction, speed and
charly 0:3f4cfbeda9d3 16 // a linear acceleration/deceleration to a Stepper Motor Controller
charly 0:3f4cfbeda9d3 17
charly 0:3f4cfbeda9d3 18 #include "TMCStepper.h"
charly 0:3f4cfbeda9d3 19
charly 0:3f4cfbeda9d3 20 // MOTOR Steps per Revolution ( 1/8 Microsteps, 200Steps per Rev / 1.8 degrees per FullStep)
charly 0:3f4cfbeda9d3 21 #define MSPR 1600
charly 0:3f4cfbeda9d3 22 // Gear Ratio
charly 0:3f4cfbeda9d3 23 #define GR 288
charly 0:3f4cfbeda9d3 24
charly 0:3f4cfbeda9d3 25 #define DRIVER_ADDRESS 0b00 // TMC2209 Driver address according to MS1 and MS2
charly 0:3f4cfbeda9d3 26 #define R_SENSE 0.11f // Match to your driver
charly 1:60419aa0c030 27 #define MICROSTEPS 32
charly 0:3f4cfbeda9d3 28
charly 0:3f4cfbeda9d3 29 //RX, TX, RS, Addr
charly 0:3f4cfbeda9d3 30 TMC2209Stepper stepper(p14, p13, R_SENSE, DRIVER_ADDRESS);
charly 0:3f4cfbeda9d3 31
charly 0:3f4cfbeda9d3 32 // Assumes little endian
charly 0:3f4cfbeda9d3 33 void printBits(size_t const size, void const * const ptr)
charly 0:3f4cfbeda9d3 34 {
charly 0:3f4cfbeda9d3 35 unsigned char *b = (unsigned char*) ptr;
charly 0:3f4cfbeda9d3 36 unsigned char byte;
charly 0:3f4cfbeda9d3 37 int i, j;
charly 0:3f4cfbeda9d3 38 // puts("#");
charly 0:3f4cfbeda9d3 39 for (i = size-1; i >= 0; i--) {
charly 0:3f4cfbeda9d3 40 for (j = 7; j >= 0; j--) {
charly 0:3f4cfbeda9d3 41 byte = (b[i] >> j) & 1;
charly 0:3f4cfbeda9d3 42 printf("%u", byte);
charly 0:3f4cfbeda9d3 43 }
charly 0:3f4cfbeda9d3 44 }
charly 0:3f4cfbeda9d3 45 // puts("#");
charly 0:3f4cfbeda9d3 46 }
charly 0:3f4cfbeda9d3 47
charly 0:3f4cfbeda9d3 48 int main()
charly 0:3f4cfbeda9d3 49 {
charly 0:3f4cfbeda9d3 50 printf("\r\nConnected to mbed\r\n");
charly 0:3f4cfbeda9d3 51 stepper.begin(); // UART: Init SW UART (if selected) with default baudrate
charly 1:60419aa0c030 52 stepper.toff(3); // Enables driver in software - 3, 5 ????
charly 1:60419aa0c030 53 stepper.rms_current(800); // Set motor RMS current in mA
charly 1:60419aa0c030 54 // 1110, 800
charly 0:3f4cfbeda9d3 55 stepper.microsteps(MICROSTEPS); // Set microsteps to 1:Fullstep ... 256: 1/256th
charly 1:60419aa0c030 56 stepper.en_spreadCycle(true); // Toggle spreadCycle on TMC2208/2209/2224: default false, true: much faster!!!!
charly 0:3f4cfbeda9d3 57 stepper.pwm_autoscale(true); // Needed for stealthChop
charly 0:3f4cfbeda9d3 58 printf("TMC-Version: %02X\r\n",stepper.version());
charly 0:3f4cfbeda9d3 59
charly 1:60419aa0c030 60 bool shaft = false; //direction CW or CCW
charly 1:60419aa0c030 61
charly 0:3f4cfbeda9d3 62 while(1) {
charly 0:3f4cfbeda9d3 63 // printf("TSTEP(): %i\r\n", stepper.TSTEP());
charly 0:3f4cfbeda9d3 64 uint32_t status = stepper.DRV_STATUS();
charly 0:3f4cfbeda9d3 65 printf("DRV_STATUS(): "); printBits(sizeof(status),&status);printf("\r\n");
charly 0:3f4cfbeda9d3 66 uint32_t ioin = stepper.IOIN();
charly 0:3f4cfbeda9d3 67 printf("IOIN(): "); printBits(sizeof(ioin),&ioin);printf("\r\n");
charly 0:3f4cfbeda9d3 68 // uint32_t otp = stepper.OTP_READ();
charly 0:3f4cfbeda9d3 69 // printf("OTP_READ(): ");printBits(sizeof(otp),&otp);printf("\r\n");
charly 0:3f4cfbeda9d3 70
charly 1:60419aa0c030 71 printf("VACTUAL(): %zu \r\n", stepper.VACTUAL());
charly 0:3f4cfbeda9d3 72 // increase
charly 1:60419aa0c030 73 uint32_t maxspeed = 3000; //max 3400 or 3000
charly 1:60419aa0c030 74 uint32_t actspeed = 0;
charly 0:3f4cfbeda9d3 75 while (actspeed < maxspeed) {
charly 0:3f4cfbeda9d3 76 actspeed += 200;
charly 1:60419aa0c030 77 if (actspeed > maxspeed) {
charly 1:60419aa0c030 78 actspeed = maxspeed;
charly 1:60419aa0c030 79 }
charly 1:60419aa0c030 80 //printf("actspeed: %i",actspeed);
charly 0:3f4cfbeda9d3 81 stepper.VACTUAL(actspeed*MICROSTEPS);// Set Speed to value
charly 1:60419aa0c030 82 ThisThread::sleep_for(25ms); //wait
charly 0:3f4cfbeda9d3 83 }
charly 0:3f4cfbeda9d3 84 printf("VACTUAL(): %zu \r\n", stepper.VACTUAL());
charly 1:60419aa0c030 85 ThisThread::sleep_for(60s);
charly 0:3f4cfbeda9d3 86 // decrease
charly 0:3f4cfbeda9d3 87 maxspeed = 0;
charly 0:3f4cfbeda9d3 88 while (actspeed > maxspeed) {
charly 0:3f4cfbeda9d3 89 actspeed -= 200;
charly 1:60419aa0c030 90 if (actspeed < 0) {
charly 1:60419aa0c030 91 actspeed = 0;
charly 1:60419aa0c030 92 }
charly 1:60419aa0c030 93 //printf("actspeed: %i",actspeed);
charly 0:3f4cfbeda9d3 94 stepper.VACTUAL(actspeed*MICROSTEPS);// Set Speed to value
charly 1:60419aa0c030 95 ThisThread::sleep_for(25ms); //wait
charly 0:3f4cfbeda9d3 96 }
charly 1:60419aa0c030 97
charly 1:60419aa0c030 98 // stepper.VACTUAL(400*MICROSTEPS);// Set Speed to value
charly 0:3f4cfbeda9d3 99 ThisThread::sleep_for(5s); //wait
charly 1:60419aa0c030 100 // inverse direction
charly 1:60419aa0c030 101 shaft = !shaft;
charly 1:60419aa0c030 102 stepper.shaft(shaft);
charly 1:60419aa0c030 103 printf("...\r\n");
charly 0:3f4cfbeda9d3 104 }
charly 0:3f4cfbeda9d3 105 }