-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotCtrl.ino
148 lines (132 loc) · 6.16 KB
/
motCtrl.ino
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// The stepper motor drive is set at 20000 steps/rev for smooth motion at lower cross-head speeds
// setup the arduino (nano used here) pins
// The motor is controlled through the serial monitor i/o
// The pulse delay to control motor is adjusted for drive steps/rev of 20000
// one need to verify the actual cross head speed and adjust the pulse delay accordingly
#include <Wire.h>
int PulsePin = 2; //Stepper pin
int DirectionPin = 3; //Direction pin
int EnablePin = 5; //Stop pinbool setdir;
volatile int cmd_id = 0; // read in from interrupt
volatile bool cmd_in = false;
int pd = 10;
int dir;
void setup(){
/*Sets all pin to output; the microcontroller will send them(the pins)
bits, it will not expect to receive any bits from thiese pins.*/
Serial.begin(9600);
pinMode(DirectionPin, OUTPUT);
pinMode(PulsePin, OUTPUT);
pinMode(EnablePin, OUTPUT);
digitalWrite(EnablePin, LOW);
//timer settings begins
cli();//stop interrupts
//set timer1 interrupt at 1Hz
TCCR1A = 0;// set entire TCCR1A register to 0
TCNT1 = 0;//initialize counter value to 0
// set compare match register for 1hz increments
OCR1A = 15624;// = (16*10^6) / (1*1024) - 1 (must be <65536)
TCCR1A |= (1 << WGM01);
// Set CS01 and CS00 bits for 64 prescaler
TCCR1A |= (1 << CS01) | (1 << CS00);
TIMSK1 |= (1 << OCIE1A);
interrupts(); // enable all interrupts
sei();//allow interrupts
}//end setup
ISR(TIMER1_COMPA_vect){
TCNT1=0;
if (Serial.available()){
cmd_in = true;
cmd_id = Serial.read()-48;
}
// digitalWrite(EnablePin, digitalRead(EnablePin) ^ 1); //make the EnablePin = 0 i.e. down
// the ISR stops the motor when a signal is available at the serial
// later I will change it to read the serial in interger - 0,1,2,3
// for 0 - it will stop the motor
// for 1 - it will move the motor fast in the forward direction
// for 2 - it will move the motor fast in the reverse direction
// for 3 - it will move the motor slow for 10^-3 strain rate
}
//timer settings ends
void loop(){
switch (cmd_id){
case 0:
digitalWrite(DirectionPin, HIGH);
digitalWrite(PulsePin,LOW);
digitalWrite(EnablePin, HIGH);
break;
case 1: // direction forward fast
pd = 1;
dir = HIGH;
// digitalWrite(EnablePin, LOW);
if (EnablePin == HIGH){digitalWrite(EnablePin, LOW);}
run_motor_slow(dir,pd);
cmd_in=false;
Serial.print("cmd_id=");
Serial.println(cmd_id);
Serial.println(cmd_in);
break;
case 2: // direction reverse fast
pd = 1;//1 s-1
dir = LOW;
// digitalWrite(EnablePin, LOW);
if (EnablePin == HIGH){digitalWrite(EnablePin, LOW);}
run_motor_slow(dir,pd);
cmd_in=false;
Serial.print("cmd_id=");
Serial.println(cmd_id);
Serial.println(cmd_in);
break;
case 3: //test at strain rates equal or slower than 0.001s-1
pd = 1;//1e-1 s-1
dir = LOW;
// digitalWrite(EnablePin, LOW);
if (EnablePin == HIGH){digitalWrite(EnablePin, LOW);}
run_motor_slow(dir,pd);
cmd_in=false;
Serial.print("cmd_id=");
Serial.println(cmd_id);
Serial.println(cmd_in);
break;
case 4: //test at strain rates equal or slower than 0.001s-1
pd = 10;//1e-2 s-1
dir = LOW;
// digitalWrite(EnablePin, LOW);
if (EnablePin == HIGH){digitalWrite(EnablePin, LOW);}
run_motor_slow(dir,pd);
cmd_in=false;
Serial.print("cmd_id=");
Serial.println(cmd_id);
Serial.println(cmd_in);
break;
case 5: //test at strain rates equal or slower than 0.001s-1
pd = 100; //1e-3 s-1
dir = LOW;
// digitalWrite(EnablePin, LOW);
if (EnablePin == HIGH){digitalWrite(EnablePin, LOW);}
run_motor_slow(dir,pd);
cmd_in=false;
Serial.print("cmd_id=");
Serial.println(cmd_id);
Serial.println(cmd_in);
break;
default:{
}
} // switch ends here
//} // while ends here
} // loop() ends here
// Motor run functions
void run_motor_slow(int setdir, int pd)
{
unsigned long previousMillis = 0;
while (!cmd_in){
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= pd)
{
previousMillis = currentMillis;
digitalWrite(DirectionPin, setdir);
digitalWrite(PulsePin,HIGH);
digitalWrite(PulsePin,LOW);
}
}
}