Testprogram for TMC2209-Library. Uses Speed-Control via VACTUAL instead of Step/Dir
Dependencies: TMCStepper mRotaryEncoder-os
main.cpp@10:376299814a3b, 2021-12-11 (annotated)
- Committer:
- charly
- Date:
- Sat Dec 11 16:44:13 2021 +0000
- Revision:
- 10:376299814a3b
- Parent:
- 9:babbfb61d347
baremetal profile
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 | 10:376299814a3b | 15 | DigitalOut ledCW(LED1); // Show rotation clockwise // Gren LED LD3 |
charly | 10:376299814a3b | 16 | DigitalOut ledCCW(NC); // Show rotation counterclockwise // no more LEDs on L432 |
charly | 5:7f250f463aa2 | 17 | |
charly | 0:3f4cfbeda9d3 | 18 | //Virtual serial port over USB with 15200 baud 8N1 |
charly | 0:3f4cfbeda9d3 | 19 | static BufferedSerial host(USBTX, USBRX,115200); |
charly | 0:3f4cfbeda9d3 | 20 | |
charly | 4:12bfa2c1729f | 21 | //mRotaryEncoder(PinName pinA, PinName pinB, PinName pinSW, PinMode pullMode=PullUp, int debounceTime_us=1000) |
charly | 10:376299814a3b | 22 | mRotaryEncoder wheel(D2, D3, D6,PullUp,3000); // default 1500 |
charly | 10:376299814a3b | 23 | //D7, D8 not available on L432KC with default solder bridges |
charly | 4:12bfa2c1729f | 24 | |
charly | 2:94c5b3f09463 | 25 | // hardware parameters: |
charly | 0:3f4cfbeda9d3 | 26 | // MOTOR Steps per Revolution ( 1/8 Microsteps, 200Steps per Rev / 1.8 degrees per FullStep) |
charly | 0:3f4cfbeda9d3 | 27 | #define MSPR 1600 |
charly | 0:3f4cfbeda9d3 | 28 | // Gear Ratio |
charly | 0:3f4cfbeda9d3 | 29 | #define GR 288 |
charly | 0:3f4cfbeda9d3 | 30 | |
charly | 0:3f4cfbeda9d3 | 31 | #define DRIVER_ADDRESS 0b00 // TMC2209 Driver address according to MS1 and MS2 |
charly | 5:7f250f463aa2 | 32 | #define R_SENSE 0.11f // R-Sense in OHM. Match to your driver |
charly | 8:f44d70665051 | 33 | #define MICROSTEPS 256 // # of microsteps |
charly | 10:376299814a3b | 34 | #define RMSCURRENT 800 // RMS current of Stepper Coil in mA |
charly | 10:376299814a3b | 35 | #define MAXSPEED 5500 // Maximaum speed (5500 with RMS800 @12V/0.6Amax) |
charly | 0:3f4cfbeda9d3 | 36 | |
charly | 2:94c5b3f09463 | 37 | // A TMCStepper-object with UART and given address and R-Sense |
charly | 0:3f4cfbeda9d3 | 38 | //RX, TX, RS, Addr |
charly | 10:376299814a3b | 39 | TMC2209Stepper stepper(D0, D1, R_SENSE, DRIVER_ADDRESS); |
charly | 0:3f4cfbeda9d3 | 40 | |
charly | 10:376299814a3b | 41 | InterruptIn diag(D12); |
charly | 10:376299814a3b | 42 | DigitalOut enn(D11); |
charly | 6:6ad7bc10ac20 | 43 | |
charly | 5:7f250f463aa2 | 44 | volatile bool enc_pressed = false; // Button of rotaryencoder was pressed |
charly | 5:7f250f463aa2 | 45 | volatile bool enc_rotated = false; // rotary encoder was totaded left or right |
charly | 5:7f250f463aa2 | 46 | volatile bool enc_action = false; // any change happened |
charly | 6:6ad7bc10ac20 | 47 | volatile bool diag_event = false; // DIAG-Pin of TMC |
charly | 4:12bfa2c1729f | 48 | int lastGet; |
charly | 4:12bfa2c1729f | 49 | int thisGet; |
charly | 4:12bfa2c1729f | 50 | |
charly | 4:12bfa2c1729f | 51 | //interrup-Handler for button on rotary-encoder |
charly | 4:12bfa2c1729f | 52 | void trigger_sw() { |
charly | 4:12bfa2c1729f | 53 | enc_pressed = true; // just set the flag, rest is done outside isr |
charly | 4:12bfa2c1729f | 54 | } |
charly | 4:12bfa2c1729f | 55 | |
charly | 4:12bfa2c1729f | 56 | //interrup-Handler for rotary-encoder rotation |
charly | 4:12bfa2c1729f | 57 | void trigger_rotated() { |
charly | 4:12bfa2c1729f | 58 | enc_rotated = true; // just set the flag, rest is done outside isr |
charly | 4:12bfa2c1729f | 59 | } |
charly | 4:12bfa2c1729f | 60 | |
charly | 6:6ad7bc10ac20 | 61 | //interrup-Handler for TMC2209-DIAG-Pin |
charly | 6:6ad7bc10ac20 | 62 | void trigger_diag() { |
charly | 6:6ad7bc10ac20 | 63 | diag_event = true; // just set the flag, rest is done outside isr |
charly | 6:6ad7bc10ac20 | 64 | } |
charly | 6:6ad7bc10ac20 | 65 | |
charly | 0:3f4cfbeda9d3 | 66 | // Assumes little endian |
charly | 0:3f4cfbeda9d3 | 67 | void printBits(size_t const size, void const * const ptr) |
charly | 0:3f4cfbeda9d3 | 68 | { |
charly | 0:3f4cfbeda9d3 | 69 | unsigned char *b = (unsigned char*) ptr; |
charly | 0:3f4cfbeda9d3 | 70 | unsigned char byte; |
charly | 0:3f4cfbeda9d3 | 71 | int i, j; |
charly | 0:3f4cfbeda9d3 | 72 | // puts("#"); |
charly | 0:3f4cfbeda9d3 | 73 | for (i = size-1; i >= 0; i--) { |
charly | 0:3f4cfbeda9d3 | 74 | for (j = 7; j >= 0; j--) { |
charly | 0:3f4cfbeda9d3 | 75 | byte = (b[i] >> j) & 1; |
charly | 0:3f4cfbeda9d3 | 76 | printf("%u", byte); |
charly | 0:3f4cfbeda9d3 | 77 | } |
charly | 0:3f4cfbeda9d3 | 78 | } |
charly | 0:3f4cfbeda9d3 | 79 | // puts("#"); |
charly | 0:3f4cfbeda9d3 | 80 | } |
charly | 0:3f4cfbeda9d3 | 81 | |
charly | 0:3f4cfbeda9d3 | 82 | int main() |
charly | 0:3f4cfbeda9d3 | 83 | { |
charly | 0:3f4cfbeda9d3 | 84 | printf("\r\nConnected to mbed\r\n"); |
charly | 10:376299814a3b | 85 | printf("This is Mbed OS %d.%d.%d.\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); |
charly | 5:7f250f463aa2 | 86 | //Intitiallize RotaryEncoder |
charly | 4:12bfa2c1729f | 87 | // call trigger_sw() when button of rotary-encoder is pressed |
charly | 4:12bfa2c1729f | 88 | wheel.attachSW(&trigger_sw); |
charly | 4:12bfa2c1729f | 89 | // call trigger_rot() when the shaft is rotaded left or right |
charly | 4:12bfa2c1729f | 90 | wheel.attachROT(&trigger_rotated); |
charly | 4:12bfa2c1729f | 91 | lastGet = 0; |
charly | 6:6ad7bc10ac20 | 92 | // set enc_rotated, so startup |
charly | 4:12bfa2c1729f | 93 | enc_rotated = true; |
charly | 5:7f250f463aa2 | 94 | enc_action = true; |
charly | 5:7f250f463aa2 | 95 | ledCW = 1; |
charly | 5:7f250f463aa2 | 96 | ledCCW = 1; |
charly | 6:6ad7bc10ac20 | 97 | |
charly | 8:f44d70665051 | 98 | // disable Driver |
charly | 8:f44d70665051 | 99 | enn=1; |
charly | 9:babbfb61d347 | 100 | // wait for Hhardware to settle |
charly | 9:babbfb61d347 | 101 | ThisThread::sleep_for(100ms); |
charly | 5:7f250f463aa2 | 102 | |
charly | 5:7f250f463aa2 | 103 | // Initialize Stepper |
charly | 5:7f250f463aa2 | 104 | printf("connecting to TMC-Module...\r\n"); |
charly | 2:94c5b3f09463 | 105 | stepper.begin(); // UART: Init SW UART (if selected) with default baudrate |
charly | 9:babbfb61d347 | 106 | |
charly | 9:babbfb61d347 | 107 | //read and check version - must be 0x21 |
charly | 9:babbfb61d347 | 108 | uint8_t tmc_version = stepper.version(); |
charly | 9:babbfb61d347 | 109 | printf("TMC-Version: %02X\r\n",tmc_version); |
charly | 9:babbfb61d347 | 110 | if (tmc_version != 0x21) { |
charly | 9:babbfb61d347 | 111 | printf("Wrong TMC-Version(not 0x21) or communication error!! STOPPING!!!\r\n"); |
charly | 9:babbfb61d347 | 112 | if (stepper.CRCerror) { |
charly | 9:babbfb61d347 | 113 | printf("CRC-Error!!!\r\n"); |
charly | 9:babbfb61d347 | 114 | } |
charly | 9:babbfb61d347 | 115 | while (1) { |
charly | 9:babbfb61d347 | 116 | ledCW = 1; |
charly | 9:babbfb61d347 | 117 | ledCCW = 0; |
charly | 9:babbfb61d347 | 118 | ThisThread::sleep_for(50ms); |
charly | 9:babbfb61d347 | 119 | ledCW = 0; |
charly | 9:babbfb61d347 | 120 | ledCCW = 1; |
charly | 9:babbfb61d347 | 121 | ThisThread::sleep_for(50ms); |
charly | 9:babbfb61d347 | 122 | }; |
charly | 9:babbfb61d347 | 123 | } |
charly | 9:babbfb61d347 | 124 | |
charly | 7:51cb60bf3e2d | 125 | stepper.push(); // initialize all registers??? required? |
charly | 7:51cb60bf3e2d | 126 | |
charly | 2:94c5b3f09463 | 127 | stepper.toff(3); // Enables driver in software - 3, 5 ???? |
charly | 2:94c5b3f09463 | 128 | stepper.rms_current(RMSCURRENT); // Set motor RMS current in mA / min 500 for 24V/speed:3000 |
charly | 1:60419aa0c030 | 129 | // 1110, 800 |
charly | 5:7f250f463aa2 | 130 | // working: 800 12V/0,6Amax, Speed up to 5200=4U/min |
charly | 5:7f250f463aa2 | 131 | |
charly | 0:3f4cfbeda9d3 | 132 | stepper.microsteps(MICROSTEPS); // Set microsteps to 1:Fullstep ... 256: 1/256th |
charly | 1:60419aa0c030 | 133 | stepper.en_spreadCycle(true); // Toggle spreadCycle on TMC2208/2209/2224: default false, true: much faster!!!! |
charly | 0:3f4cfbeda9d3 | 134 | stepper.pwm_autoscale(true); // Needed for stealthChop |
charly | 0:3f4cfbeda9d3 | 135 | |
charly | 8:f44d70665051 | 136 | // enable driver |
charly | 8:f44d70665051 | 137 | enn = 0; |
charly | 8:f44d70665051 | 138 | |
charly | 6:6ad7bc10ac20 | 139 | uint8_t gstat = stepper.GSTAT(); |
charly | 6:6ad7bc10ac20 | 140 | printf("GSTAT(): "); printBits(sizeof(gstat),&gstat);printf("\r\n"); |
charly | 6:6ad7bc10ac20 | 141 | |
charly | 8:f44d70665051 | 142 | uint32_t gconf = stepper.GCONF(); |
charly | 8:f44d70665051 | 143 | printf("GCONF(): "); printBits(sizeof(gconf),&gconf);printf("\r\n"); |
charly | 8:f44d70665051 | 144 | |
charly | 5:7f250f463aa2 | 145 | uint32_t status = stepper.DRV_STATUS(); |
charly | 5:7f250f463aa2 | 146 | printf("DRV_STATUS(): "); printBits(sizeof(status),&status);printf("\r\n"); |
charly | 5:7f250f463aa2 | 147 | |
charly | 6:6ad7bc10ac20 | 148 | uint32_t ioin = stepper.IOIN(); |
charly | 6:6ad7bc10ac20 | 149 | printf("IOIN(): "); printBits(sizeof(ioin),&ioin);printf("\r\n"); |
charly | 6:6ad7bc10ac20 | 150 | |
charly | 8:f44d70665051 | 151 | uint8_t ihold = stepper.ihold(); |
charly | 8:f44d70665051 | 152 | printf("IHOLD(): "); printBits(sizeof(ihold),&ihold);printf("\r\n"); |
charly | 8:f44d70665051 | 153 | |
charly | 8:f44d70665051 | 154 | uint8_t irun = stepper.irun(); |
charly | 8:f44d70665051 | 155 | printf("IRUN(): "); printBits(sizeof(irun),&irun);printf("\r\n"); |
charly | 8:f44d70665051 | 156 | |
charly | 8:f44d70665051 | 157 | uint8_t iholddelay = stepper.iholddelay(); |
charly | 8:f44d70665051 | 158 | printf("IHOLDDELAY(): "); printBits(sizeof(iholddelay),&iholddelay);printf("\r\n"); |
charly | 8:f44d70665051 | 159 | |
charly | 8:f44d70665051 | 160 | // do a peep by setting vactual to a too high speed |
charly | 8:f44d70665051 | 161 | |
charly | 9:babbfb61d347 | 162 | //stepper.VACTUAL(50000*MICROSTEPS); |
charly | 9:babbfb61d347 | 163 | //ThisThread::sleep_for(1s); |
charly | 8:f44d70665051 | 164 | |
charly | 8:f44d70665051 | 165 | // initialize Automatic tunig (Chap 6.1) |
charly | 9:babbfb61d347 | 166 | printf("Start automatic tunig Chap6.1 ....."); |
charly | 8:f44d70665051 | 167 | stepper.VACTUAL(1); |
charly | 8:f44d70665051 | 168 | stepper.VACTUAL(0); |
charly | 8:f44d70665051 | 169 | ThisThread::sleep_for(100ms); |
charly | 8:f44d70665051 | 170 | stepper.VACTUAL(500*MICROSTEPS); |
charly | 8:f44d70665051 | 171 | ThisThread::sleep_for(2s); |
charly | 8:f44d70665051 | 172 | stepper.VACTUAL(0); |
charly | 9:babbfb61d347 | 173 | printf("done\r\n"); |
charly | 8:f44d70665051 | 174 | |
charly | 7:51cb60bf3e2d | 175 | diag.rise(&trigger_diag); |
charly | 7:51cb60bf3e2d | 176 | |
charly | 5:7f250f463aa2 | 177 | //bool shaft = false; //direction CW or CCW |
charly | 1:60419aa0c030 | 178 | |
charly | 0:3f4cfbeda9d3 | 179 | while(1) { |
charly | 5:7f250f463aa2 | 180 | /* Spped-UP/Down-Cycles |
charly | 0:3f4cfbeda9d3 | 181 | // printf("TSTEP(): %i\r\n", stepper.TSTEP()); |
charly | 0:3f4cfbeda9d3 | 182 | uint32_t status = stepper.DRV_STATUS(); |
charly | 0:3f4cfbeda9d3 | 183 | printf("DRV_STATUS(): "); printBits(sizeof(status),&status);printf("\r\n"); |
charly | 0:3f4cfbeda9d3 | 184 | uint32_t ioin = stepper.IOIN(); |
charly | 0:3f4cfbeda9d3 | 185 | printf("IOIN(): "); printBits(sizeof(ioin),&ioin);printf("\r\n"); |
charly | 0:3f4cfbeda9d3 | 186 | // uint32_t otp = stepper.OTP_READ(); |
charly | 0:3f4cfbeda9d3 | 187 | // printf("OTP_READ(): ");printBits(sizeof(otp),&otp);printf("\r\n"); |
charly | 0:3f4cfbeda9d3 | 188 | |
charly | 1:60419aa0c030 | 189 | printf("VACTUAL(): %zu \r\n", stepper.VACTUAL()); |
charly | 0:3f4cfbeda9d3 | 190 | // increase |
charly | 1:60419aa0c030 | 191 | uint32_t maxspeed = 3000; //max 3400 or 3000 |
charly | 1:60419aa0c030 | 192 | uint32_t actspeed = 0; |
charly | 0:3f4cfbeda9d3 | 193 | while (actspeed < maxspeed) { |
charly | 0:3f4cfbeda9d3 | 194 | actspeed += 200; |
charly | 1:60419aa0c030 | 195 | if (actspeed > maxspeed) { |
charly | 1:60419aa0c030 | 196 | actspeed = maxspeed; |
charly | 1:60419aa0c030 | 197 | } |
charly | 3:209a9c414f54 | 198 | printf("actspeed: %i",actspeed); |
charly | 0:3f4cfbeda9d3 | 199 | stepper.VACTUAL(actspeed*MICROSTEPS);// Set Speed to value |
charly | 1:60419aa0c030 | 200 | ThisThread::sleep_for(25ms); //wait |
charly | 0:3f4cfbeda9d3 | 201 | } |
charly | 0:3f4cfbeda9d3 | 202 | printf("VACTUAL(): %zu \r\n", stepper.VACTUAL()); |
charly | 3:209a9c414f54 | 203 | ThisThread::sleep_for(5s); |
charly | 0:3f4cfbeda9d3 | 204 | // decrease |
charly | 0:3f4cfbeda9d3 | 205 | maxspeed = 0; |
charly | 0:3f4cfbeda9d3 | 206 | while (actspeed > maxspeed) { |
charly | 0:3f4cfbeda9d3 | 207 | actspeed -= 200; |
charly | 1:60419aa0c030 | 208 | if (actspeed < 0) { |
charly | 1:60419aa0c030 | 209 | actspeed = 0; |
charly | 1:60419aa0c030 | 210 | } |
charly | 3:209a9c414f54 | 211 | printf("actspeed: %i",actspeed); |
charly | 0:3f4cfbeda9d3 | 212 | stepper.VACTUAL(actspeed*MICROSTEPS);// Set Speed to value |
charly | 1:60419aa0c030 | 213 | ThisThread::sleep_for(25ms); //wait |
charly | 0:3f4cfbeda9d3 | 214 | } |
charly | 1:60419aa0c030 | 215 | |
charly | 1:60419aa0c030 | 216 | // stepper.VACTUAL(400*MICROSTEPS);// Set Speed to value |
charly | 0:3f4cfbeda9d3 | 217 | ThisThread::sleep_for(5s); //wait |
charly | 1:60419aa0c030 | 218 | // inverse direction |
charly | 1:60419aa0c030 | 219 | shaft = !shaft; |
charly | 1:60419aa0c030 | 220 | stepper.shaft(shaft); |
charly | 2:94c5b3f09463 | 221 | // Read Interace-Count |
charly | 2:94c5b3f09463 | 222 | printf("IFCNT(): %zu \r\n",stepper.IFCNT()); |
charly | 1:60419aa0c030 | 223 | printf("...\r\n"); |
charly | 4:12bfa2c1729f | 224 | */ |
charly | 5:7f250f463aa2 | 225 | ////// Control motor-speed by rotary-encoder |
charly | 5:7f250f463aa2 | 226 | |
charly | 6:6ad7bc10ac20 | 227 | // DIAG-PIN showed Error-Condition? |
charly | 6:6ad7bc10ac20 | 228 | if (diag_event) { |
charly | 6:6ad7bc10ac20 | 229 | diag_event = false; |
charly | 6:6ad7bc10ac20 | 230 | printf("DIAG occured!\r\n"); |
charly | 6:6ad7bc10ac20 | 231 | gstat = stepper.GSTAT(); |
charly | 6:6ad7bc10ac20 | 232 | printf("GSTAT(): "); printBits(sizeof(gstat),&gstat);printf("\r\n"); |
charly | 6:6ad7bc10ac20 | 233 | status = stepper.DRV_STATUS(); |
charly | 6:6ad7bc10ac20 | 234 | printf("DRV_STATUS(): "); printBits(sizeof(status),&status);printf("\r\n"); |
charly | 6:6ad7bc10ac20 | 235 | //safty turn off friver |
charly | 6:6ad7bc10ac20 | 236 | printf("Shutting Down Motordriver...\r\n"); |
charly | 8:f44d70665051 | 237 | enn=1; |
charly | 6:6ad7bc10ac20 | 238 | stepper.toff(0); |
charly | 6:6ad7bc10ac20 | 239 | status = stepper.DRV_STATUS(); |
charly | 6:6ad7bc10ac20 | 240 | printf("DRV_STATUS(): "); printBits(sizeof(status),&status);printf("\r\n"); |
charly | 6:6ad7bc10ac20 | 241 | ioin = stepper.IOIN(); |
charly | 6:6ad7bc10ac20 | 242 | printf("IOIN(): "); printBits(sizeof(ioin),&ioin);printf("\r\n"); |
charly | 8:f44d70665051 | 243 | ihold = stepper.ihold(); |
charly | 8:f44d70665051 | 244 | printf("IHOLD(): "); printBits(sizeof(ihold),&ihold);printf("\r\n"); |
charly | 8:f44d70665051 | 245 | irun = stepper.irun(); |
charly | 8:f44d70665051 | 246 | printf("IRUN(): "); printBits(sizeof(irun),&irun);printf("\r\n"); |
charly | 8:f44d70665051 | 247 | iholddelay = stepper.iholddelay(); |
charly | 8:f44d70665051 | 248 | printf("IHOLDDELAY(): "); printBits(sizeof(iholddelay),&iholddelay);printf("\r\n"); |
charly | 6:6ad7bc10ac20 | 249 | printf("stopping programm - manual RESET required!!!!!!\r\n"); |
charly | 8:f44d70665051 | 250 | while (1) { |
charly | 8:f44d70665051 | 251 | ledCW = 1; |
charly | 8:f44d70665051 | 252 | ledCCW = 1; |
charly | 8:f44d70665051 | 253 | ThisThread::sleep_for(200ms); |
charly | 8:f44d70665051 | 254 | ledCW = 0; |
charly | 8:f44d70665051 | 255 | ledCCW = 0; |
charly | 8:f44d70665051 | 256 | ThisThread::sleep_for(200ms); |
charly | 8:f44d70665051 | 257 | }; |
charly | 6:6ad7bc10ac20 | 258 | |
charly | 6:6ad7bc10ac20 | 259 | } |
charly | 6:6ad7bc10ac20 | 260 | |
charly | 4:12bfa2c1729f | 261 | // shaft has been rotated? |
charly | 4:12bfa2c1729f | 262 | if (enc_rotated) { |
charly | 4:12bfa2c1729f | 263 | enc_rotated = false; |
charly | 5:7f250f463aa2 | 264 | enc_action = true; |
charly | 4:12bfa2c1729f | 265 | thisGet = wheel.Get(); |
charly | 5:7f250f463aa2 | 266 | if (thisGet*100 > MAXSPEED) { //on upper limit? |
charly | 5:7f250f463aa2 | 267 | wheel.Set( MAXSPEED/100); |
charly | 5:7f250f463aa2 | 268 | thisGet = wheel.Get(); |
charly | 5:7f250f463aa2 | 269 | } |
charly | 5:7f250f463aa2 | 270 | if (thisGet*100 < MAXSPEED*(-1)) { //on lower limit? |
charly | 5:7f250f463aa2 | 271 | wheel.Set( MAXSPEED*(-1)/100); |
charly | 5:7f250f463aa2 | 272 | thisGet = wheel.Get(); |
charly | 5:7f250f463aa2 | 273 | } |
charly | 4:12bfa2c1729f | 274 | stepper.VACTUAL(thisGet*100*MICROSTEPS);// Set Speed to value |
charly | 4:12bfa2c1729f | 275 | printf("actspeed: %i\r\n",thisGet*100); |
charly | 5:7f250f463aa2 | 276 | |
charly | 4:12bfa2c1729f | 277 | } |
charly | 4:12bfa2c1729f | 278 | // Button pressed? |
charly | 4:12bfa2c1729f | 279 | if (enc_pressed) { |
charly | 4:12bfa2c1729f | 280 | enc_pressed = false; |
charly | 5:7f250f463aa2 | 281 | enc_action = true; |
charly | 4:12bfa2c1729f | 282 | wheel.Set(0); |
charly | 4:12bfa2c1729f | 283 | thisGet = wheel.Get(); |
charly | 4:12bfa2c1729f | 284 | stepper.VACTUAL(thisGet*100*MICROSTEPS);// Set Speed to value |
charly | 4:12bfa2c1729f | 285 | printf("actspeed: %i\r\n",thisGet*100); |
charly | 7:51cb60bf3e2d | 286 | |
charly | 7:51cb60bf3e2d | 287 | gstat = stepper.GSTAT(); |
charly | 7:51cb60bf3e2d | 288 | printf("GSTAT(): "); printBits(sizeof(gstat),&gstat);printf("\r\n"); |
charly | 8:f44d70665051 | 289 | gconf = stepper.GCONF(); |
charly | 8:f44d70665051 | 290 | printf("GCONF(): "); printBits(sizeof(gconf),&gconf);printf("\r\n"); |
charly | 7:51cb60bf3e2d | 291 | status = stepper.DRV_STATUS(); |
charly | 7:51cb60bf3e2d | 292 | printf("DRV_STATUS(): "); printBits(sizeof(status),&status);printf("\r\n"); |
charly | 7:51cb60bf3e2d | 293 | ioin = stepper.IOIN(); |
charly | 7:51cb60bf3e2d | 294 | printf("IOIN(): "); printBits(sizeof(ioin),&ioin);printf("\r\n"); |
charly | 8:f44d70665051 | 295 | ihold = stepper.ihold(); |
charly | 8:f44d70665051 | 296 | printf("IHOLD(): "); printBits(sizeof(ihold),&ihold);printf("\r\n"); |
charly | 8:f44d70665051 | 297 | irun = stepper.irun(); |
charly | 8:f44d70665051 | 298 | printf("IRUN(): "); printBits(sizeof(irun),&irun);printf("\r\n"); |
charly | 8:f44d70665051 | 299 | iholddelay = stepper.iholddelay(); |
charly | 8:f44d70665051 | 300 | printf("IHOLDDELAY(): "); printBits(sizeof(iholddelay),&iholddelay);printf("\r\n"); |
charly | 4:12bfa2c1729f | 301 | |
charly | 4:12bfa2c1729f | 302 | } |
charly | 5:7f250f463aa2 | 303 | // anything changed? |
charly | 5:7f250f463aa2 | 304 | if (enc_action) { |
charly | 5:7f250f463aa2 | 305 | enc_action = false; |
charly | 5:7f250f463aa2 | 306 | // show direction of motor on leds |
charly | 5:7f250f463aa2 | 307 | if (thisGet > 0) { |
charly | 5:7f250f463aa2 | 308 | ledCW = 1; |
charly | 5:7f250f463aa2 | 309 | ledCCW= 0; |
charly | 5:7f250f463aa2 | 310 | } |
charly | 5:7f250f463aa2 | 311 | if (thisGet < 0) { |
charly | 5:7f250f463aa2 | 312 | ledCW = 0; |
charly | 5:7f250f463aa2 | 313 | ledCCW= 1; |
charly | 5:7f250f463aa2 | 314 | } |
charly | 5:7f250f463aa2 | 315 | if (thisGet == 0) { |
charly | 5:7f250f463aa2 | 316 | ledCW = 1; |
charly | 5:7f250f463aa2 | 317 | ledCCW= 1; |
charly | 5:7f250f463aa2 | 318 | } |
charly | 5:7f250f463aa2 | 319 | } |
charly | 4:12bfa2c1729f | 320 | } // while 1 |
charly | 0:3f4cfbeda9d3 | 321 | } |