-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathard_control.ino
More file actions
131 lines (106 loc) · 5.37 KB
/
Copy pathard_control.ino
File metadata and controls
131 lines (106 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Program demonstrating how to control a powerSTEP01-based ST X-NUCLEO-IHM03A1
// stepper motor driver shield on an Arduino Uno-compatible board
#include <Ponoor_PowerSTEP01Library.h>
#include <SPI.h>
// Pin definitions for the X-NUCLEO-IHM03A1 connected to an Uno-compatible board
#define nCS_PIN 10
#define STCK_PIN 9
#define nSTBY_nRESET_PIN 8
#define nBUSY_PIN 4
//Control Pin inputs
#define motor_fwd 7
#define motor_rev 6
// powerSTEP library instance, parameters are distance from the end of a daisy-chain
// of drivers, !CS pin, !STBY/!Reset pin
powerSTEP driver(0, nCS_PIN, nSTBY_nRESET_PIN);
void setup()
{
// Start serial
Serial.begin(9600);
Serial.println("powerSTEP01 Arduino control initialising...");
// Prepare pins
pinMode(nSTBY_nRESET_PIN, OUTPUT);
pinMode(nCS_PIN, OUTPUT);
pinMode(MOSI, OUTPUT);
pinMode(MISO, OUTPUT);
pinMode(SCK, OUTPUT);
// Reset powerSTEP and set CS
digitalWrite(nSTBY_nRESET_PIN, HIGH);
digitalWrite(nSTBY_nRESET_PIN, LOW);
digitalWrite(nSTBY_nRESET_PIN, HIGH);
digitalWrite(nCS_PIN, HIGH);
// Start SPI
//SPI.begin();
//SPI.setDataMode(SPI_MODE3);
SPISettings mySetting(14000000, MSBFIRST, SPI_MODE3);
SPI.beginTransaction(mySetting);
// Configure powerSTEP
driver.SPIPortConnect(&SPI); // give library the SPI port (only the one on an Uno)
driver.configSyncPin(BUSY_PIN, 0); // use SYNC/nBUSY pin as nBUSY,
// thus syncSteps (2nd paramater) does nothing
driver.configStepMode(STEP_FS_128); // 1/128 microstepping, full steps = STEP_FS,
// options: 1, 1/2, 1/4, 1/8, 1/16, 1/32, 1/64, 1/128
driver.setMaxSpeed(1000); // max speed in units of full steps/s
driver.setFullSpeed(2000); // full steps/s threshold for disabling microstepping
driver.setAcc(2000); // full steps/s^2 acceleration
driver.setDec(2000); // full steps/s^2 deceleration
driver.setSlewRate(SR_520V_us); // faster may give more torque (but also EM noise),
// options are: 114, 220, 400, 520, 790, 980(V/us)
driver.setOCThreshold(8); // over-current threshold for the 2.8A NEMA23 motor
// used in testing. If your motor stops working for
// no apparent reason, it's probably this. Start low
// and increase until it doesn't trip, then maybe
// add one to avoid misfires. Can prevent catastrophic
// failures caused by shorts
driver.setOCShutdown(OC_SD_ENABLE); // shutdown motor bridge on over-current event
// to protect against permanant damage
driver.setPWMFreq(PWM_DIV_1, PWM_MUL_0_75); // 16MHz*0.75/(512*1) = 23.4375kHz
// power is supplied to stepper phases as a sin wave,
// frequency is set by two PWM modulators,
// Fpwm = Fosc*m/(512*N), N and m are set by DIV and MUL,
// options: DIV: 1, 2, 3, 4, 5, 6, 7,
// MUL: 0.625, 0.75, 0.875, 1, 1.25, 1.5, 1.75, 2
driver.setVoltageComp(VS_COMP_DISABLE); // no compensation for variation in Vs as
// ADC voltage divider is not populated
driver.setSwitchMode(SW_USER); // switch doesn't trigger stop, status can be read.
// SW_HARD_STOP: TP1 causes hard stop on connection
// to GND, you get stuck on switch after homing
driver.setOscMode(INT_16MHZ); // 16MHz internal oscillator as clock source
// KVAL registers set the power to the motor by adjusting the PWM duty cycle,
// use a value between 0-255 where 0 = no power, 255 = full power.
// Start low and monitor the motor temperature until you find a safe balance
// between power and temperature. Only use what you need
driver.setRunKVAL(64);
driver.setAccKVAL(64);
driver.setDecKVAL(64);
driver.setHoldKVAL(8);
driver.setParam(ALARM_EN, 0x8F); // disable ADC UVLO (divider not populated),
// disable stall detection (not configured),
// disable switch (not using as hard stop)
driver.getStatus(); // clears error flags
Serial.println(F("Initialisation complete"));
}
void loop()
{
Serial.println((int)driver.getStatus(), HEX); // print STATUS register
#define Full_Turn 12800 //at STEP_FS_128
#define Half_Turn 6400 //at STEP_FS_128
#define Quarter_Turn 3200 //at STEP_FS_128
#define Single_Step 128
if (digitalRead(7) == HIGH) {
driver.move(FWD, Single_Step); // move forward 25600 microsteps
while(driver.busyCheck()); // wait fo the move to finish
driver.softStop(); // soft stops prevent errors in the next operation
while(driver.busyCheck());
delay(2);
}
if (digitalRead(6) == HIGH) {
driver.move(REV, Single_Step); // move forward 25600 microsteps
while(driver.busyCheck()); // wait fo the move to finish
driver.softStop(); // soft stops prevent errors in the next operation
while(driver.busyCheck());
delay(2);
}
Serial.println((int)driver.getStatus(), HEX);
delay(2);
}