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

Dependencies:   TMCStepper mRotaryEncoder-os

main.cpp

Committer:
charly
Date:
2021-02-04
Revision:
0:3f4cfbeda9d3
Child:
1:60419aa0c030

File content as of revision 0:3f4cfbeda9d3:

/* mbed Microcontroller Library
 * Copyright (c) 2019 ARM Limited
 * SPDX-License-Identifier: Apache-2.0
 */

#include "mbed.h"
#include "platform/mbed_thread.h"


DigitalOut ledDir(LED2);
//Virtual serial port over USB with 15200 baud 8N1
static BufferedSerial host(USBTX, USBRX,115200);


// apply number of steps, direction, speed and 
// a linear acceleration/deceleration to a Stepper Motor Controller

#include "TMCStepper.h"
 
// MOTOR Steps per Revolution ( 1/8 Microsteps, 200Steps per Rev / 1.8 degrees per FullStep)
#define MSPR 1600
// Gear Ratio
#define GR 288

#define DRIVER_ADDRESS 0b00 // TMC2209 Driver address according to MS1 and MS2
#define R_SENSE 0.11f // Match to your driver
#define MICROSTEPS 64

//RX, TX, RS, Addr 
TMC2209Stepper stepper(p14, p13, R_SENSE, DRIVER_ADDRESS);

// Assumes little endian
void printBits(size_t const size, void const * const ptr)
{
    unsigned char *b = (unsigned char*) ptr;
    unsigned char byte;
    int i, j;
//    puts("#");
    for (i = size-1; i >= 0; i--) {
        for (j = 7; j >= 0; j--) {
            byte = (b[i] >> j) & 1;
            printf("%u", byte);
        }
    }
//    puts("#");
}
 
 int main()
 {
    printf("\r\nConnected to mbed\r\n");
    stepper.begin();                  // UART: Init SW UART (if selected) with default baudrate
    stepper.toff(5);                  // Enables driver in software
    stepper.rms_current(600);         // Set motor RMS current in mA
    stepper.microsteps(MICROSTEPS);   // Set microsteps to 1:Fullstep ... 256: 1/256th
    stepper.en_spreadCycle(false);    // Toggle spreadCycle on TMC2208/2209/2224
    stepper.pwm_autoscale(true);      // Needed for stealthChop
    printf("TMC-Version: %02X\r\n",stepper.version());
    
    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");
        uint32_t ioin = stepper.IOIN();        
        printf("IOIN(): "); printBits(sizeof(ioin),&ioin);printf("\r\n");
//        uint32_t otp = stepper.OTP_READ();
//        printf("OTP_READ(): ");printBits(sizeof(otp),&otp);printf("\r\n");
      
/*        printf("VACTUAL(): %zu \r\n", stepper.VACTUAL());
        // increase
        int maxspeed = 1600;
        int actspeed = 0;
        while (actspeed < maxspeed) {
            actspeed += 200;
            printf("actspeed: %i",actspeed);
            stepper.VACTUAL(actspeed*MICROSTEPS);// Set Speed to value
            ThisThread::sleep_for(20ms); //wait
        }
        printf("VACTUAL(): %zu \r\n", stepper.VACTUAL());
        ThisThread::sleep_for(10s); 
        // decrease
        maxspeed = 0;
        while (actspeed > maxspeed) {
            actspeed -= 200;
            printf("actspeed: %i",actspeed);
            stepper.VACTUAL(actspeed*MICROSTEPS);// Set Speed to value
            ThisThread::sleep_for(20ms); //wait
        }
*/
        stepper.VACTUAL(400*MICROSTEPS);// Set Speed to value
        ThisThread::sleep_for(5s); //wait 
        printf("loop...\r\n");
   }
 }