Progetto

Generale

Profilo

File » TIRL_Trinket_TEST.ino

TIRL_Trinket_TEST - Andrea Cimini, 18-11-2015 23:29

 
1
// Download and install Trinket Pro drivers from
2
// https://learn.adafruit.com/introducing-pro-trinket/starting-the-bootloader
3

    
4
// [Tools] -> [Programmer] -> "USBtinyISP"
5
// [Tools] -> [Board] -> "Pro Trinket 3V/12 Mhz (USB)"
6

    
7

    
8
#include <Servo.h>
9

    
10
// setup servo
11
int servoPin = 8;
12
int PEN_DOWN = 170; // angle of servo when pen is down
13
int PEN_UP = 80;   // angle of servo when pen is up
14
Servo penServo;
15

    
16
//int wheel_dia=66.25; //      # mm (increase = spiral out)
17
int wheel_dia=64.5; //      # mm (increase = spiral out)
18
//int wheel_base=112; //,    # mm (increase = spiral in) 
19
int wheel_base=114.5; //,    # mm (increase = spiral in) 
20
//int steps_rev=128; //,     # 512 for 64x gearbox, 128 for 16x gearbox
21
int steps_rev=512; //,     # 512 for 64x gearbox, 128 for 16x gearbox
22
int delay_time=6; //            # time between steps in ms
23

    
24
// Stepper sequence org->pink->blue->yel
25
int L_stepper_pins[] = {10, 12, 13, 11};
26
int R_stepper_pins[] = {3, 5, 6, 4};
27

    
28
int fwd_mask[][4] =  {{1, 0, 1, 0},
29
                      {0, 1, 1, 0},
30
                      {0, 1, 0, 1},
31
                      {1, 0, 0, 1}};
32

    
33
int rev_mask[][4] =  {{1, 0, 0, 1},
34
                      {0, 1, 0, 1},
35
                      {0, 1, 1, 0},
36
                      {1, 0, 1, 0}};
37

    
38

    
39
void setup() {
40
  randomSeed(analogRead(1)); 
41
  // put your setup code here, to run once:
42
  Serial.begin(9600);
43
  for(int pin=0; pin<4; pin++){
44
    pinMode(L_stepper_pins[pin], OUTPUT);
45
    digitalWrite(L_stepper_pins[pin], LOW);
46
    pinMode(R_stepper_pins[pin], OUTPUT);
47
    digitalWrite(R_stepper_pins[pin], LOW);
48
  }
49
  penServo.attach(servoPin);
50
  Serial.println("setup");
51
}
52

    
53

    
54
void loop(){ // draw a calibration box 4 times
55
  pendown();
56
  for(int x=0; x<12; x++){
57
    forward(100);
58
    left(90);
59
  }
60
  penup();
61
  done();      // releases stepper motor
62
  while(1);    // wait for reset
63
}
64

    
65

    
66
// ----- HELPER FUNCTIONS -----------
67
int step(float distance){
68
  int steps = distance * steps_rev / (wheel_dia * 3.1412); //24.61
69
  /*
70
  Serial.print(distance);
71
  Serial.print(" ");
72
  Serial.print(steps_rev);
73
  Serial.print(" ");  
74
  Serial.print(wheel_dia);
75
  Serial.print(" ");  
76
  Serial.println(steps);
77
  delay(1000);*/
78
  return steps;  
79
}
80

    
81

    
82
void forward(float distance){
83
  int steps = step(distance);
84
  Serial.println(steps);
85
  for(int step=0; step<steps; step++){
86
    for(int mask=0; mask<4; mask++){
87
      for(int pin=0; pin<4; pin++){
88
        digitalWrite(L_stepper_pins[pin], rev_mask[mask][pin]);
89
        digitalWrite(R_stepper_pins[pin], fwd_mask[mask][pin]);
90
      }
91
      delay(delay_time);
92
    } 
93
  }
94
}
95

    
96

    
97
void backward(float distance){
98
  int steps = step(distance);
99
  for(int step=0; step<steps; step++){
100
    for(int mask=0; mask<4; mask++){
101
      for(int pin=0; pin<4; pin++){
102
        digitalWrite(L_stepper_pins[pin], fwd_mask[mask][pin]);
103
        digitalWrite(R_stepper_pins[pin], rev_mask[mask][pin]);
104
      }
105
      delay(delay_time);
106
    } 
107
  }
108
}
109

    
110

    
111
void right(float degrees){
112
  float rotation = degrees / 360.0;
113
  float distance = wheel_base * 3.1412 * rotation;
114
  int steps = step(distance);
115
  for(int step=0; step<steps; step++){
116
    for(int mask=0; mask<4; mask++){
117
      for(int pin=0; pin<4; pin++){
118
        digitalWrite(R_stepper_pins[pin], rev_mask[mask][pin]);
119
        digitalWrite(L_stepper_pins[pin], rev_mask[mask][pin]);
120
      }
121
      delay(delay_time);
122
    } 
123
  }   
124
}
125

    
126

    
127
void left(float degrees){
128
  float rotation = degrees / 360.0;
129
  float distance = wheel_base * 3.1412 * rotation;
130
  int steps = step(distance);
131
  for(int step=0; step<steps; step++){
132
    for(int mask=0; mask<4; mask++){
133
      for(int pin=0; pin<4; pin++){
134
        digitalWrite(R_stepper_pins[pin], fwd_mask[mask][pin]);
135
        digitalWrite(L_stepper_pins[pin], fwd_mask[mask][pin]);
136
      }
137
      delay(delay_time);
138
    } 
139
  }   
140
}
141

    
142

    
143
void done(){ // unlock stepper to save battery
144
  for(int mask=0; mask<4; mask++){
145
    for(int pin=0; pin<4; pin++){
146
      digitalWrite(R_stepper_pins[pin], LOW);
147
      digitalWrite(L_stepper_pins[pin], LOW);
148
    }
149
    delay(delay_time);
150
  }
151
}
152

    
153

    
154
void penup(){
155
  delay(250);
156
  Serial.println("PEN_UP()");
157
  penServo.write(PEN_UP);
158
  delay(250);
159
}
160

    
161

    
162
void pendown(){
163
  delay(250);  
164
  Serial.println("PEN_DOWN()");
165
  penServo.write(PEN_DOWN);
166
  delay(250);
167
}
168

    
(12-12/38)