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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
| // myfilterwheel example
// ONLY FOR USE WITH 28BYJ-48 AND ULN2003 DRIVER
// v1.00_ULN2003 25032016
// Initial code
#include <Arduino.h>
#include <Stepper.h> // needed for stepper motor
#include <EEPROM.h> // needed for EEPROM
#include "eepromanything.h" // needed for EEPROM
// #define EEPROMSIZE 512 // ATMEGA168 512 EEPROM
#define EEPROMSIZE 1024 // ATMEGA328P 1024 EEPROM
#define FRED 1
#define FGREEN 2
#define FBLUE 3
#define FLUMIN 4
#define FOTHER 5
// these are stored in EEPROM - all variables in a structure
struct config_t
{
int validdata;
int slots[5]; // slot[0] = home, this is a 5 position filter wheel, numbered 0 to 4, each holds a color value
int steps[5] = { 0, 1000, 2000, -2000, -1000 };
} myfilter;
// will hold size of the struct myfocuser - 6 bytes
int datasize; // will hold size of the struct myfocuser - 6 bytes
int nlocations; // number of storage locations available in EEPROM
int currentaddr; // will be address in eeprom of the data stored
bool writenow; // should we update values in eeprom
bool found; // did we find any stored values?
long previousMillis = 0L; // used as a delay whenever the EEPROM settings need to be updated
long interval = 10000L; // interval in milliseconds to wait after a move before writing settings to EEPROM, 30s
const String programName = "myFilter_ULN2003";
const String programVersion = "1.0.0";
const int stepsPerRevolution = 2048; // there are 2048 steps for one rotation of the 28BYJ-48 shaft
// initialize the stepper library on pins 4 (IN1), 5 (IN2), 6 (IN3), 7 (IN4)
Stepper mystepper(stepsPerRevolution, 4, 6, 7, 5);
// note that the stepper does not release current to the coils after a step so it gets hot
// the code below used by clearoutputs() releases the stepper after a step preventing it from getting hot
int motorPins[] = { 4, 5, 6, 7 }; // used to disable the output so it does not overheat
int motorSpeed = 2;
long maxsteps = 3000L; // default maximum number of steps of filter wheel
int currentslotnumber = 0;
int targetslotnumber = 0;
int homepin = 2; // this is where the index for home is connected to D2 pin
#define dirclockwise 1
#define diranticlockwise 2
char inChar;
boolean isMoving = false; // is the motor currently moving
long pos;
#define MAXCOMMAND 8
char mycmd[MAXCOMMAND];
char param[MAXCOMMAND];
char line[MAXCOMMAND];
int eoc = 0; // end of command
int idx = 0; // index into command string
// disable the stepper motor outputs
void clearOutput()
{
for (int i = 0; i < 4; i++)
{
digitalWrite(motorPins[i], 0);
}
}
// Move stepper anticlockwise
void anticlockwise()
{
mystepper.step(-1); // step the motor one step anticlockwise
}
// Move stepper clockwise
void clockwise()
{
mystepper.step(1); // step the motor one step clockwise
}
void findindex()
{
// this homes the filter wheel to slot 0
// it uses a hall-effect or magnet which gives true on Pin2 when its home
// avoid going too fast else will miss the home signal
int steps = 0;
int mydirection = dirclockwise;
while ( digitalRead( homepin ) == false )
{
if ( mydirection == dirclockwise )
{
clockwise(); // move the filter wheel
steps++;
if ( steps > maxsteps )
{
steps = 0;
mydirection = diranticlockwise;
}
}
else if ( mydirection == diranticlockwise )
{
anticlockwise();
steps--;
if ( steps < 0 )
{
steps = 0;
mydirection = dirclockwise;
}
}
}
// found signal, so
// ismoving = false;
currentslotnumber = 0;
targetslotnumber = 0;
}
void movetoslotnumber( int slottomoveto )
{
targetslotnumber = slottomoveto;
// check if we are already there
if ( targetslotnumber == currentslotnumber )
{
// we are already there
// so do nothing
}
else
{
// we are not there so move
// work out direction and number of steps
// yet to finish this code
}
}
// ASCOM Serial Commands
void processCommand(String command)
{
memset(mycmd, 0, MAXCOMMAND);
memset(param, 0, MAXCOMMAND);
int len = strlen(line);
if (len >= 2)
{
strncpy(mycmd, line, 2);
}
if (len > 2)
{
strncpy(param, line + 2, len - 2);
}
memset(line, 0, MAXCOMMAND);
eoc = 0;
idx = 0;
// get the current filter position
if (!strcasecmp(mycmd, "GP"))
{
Serial.print("P");
Serial.print(currentslotnumber);
Serial.print("#");
}
// motor is moving - 1 if moving, 0 otherwise
else if (!strcasecmp(mycmd, "GI"))
{
if (isMoving )
{
Serial.print("I01#");
}
else {
Serial.print("I00#");
}
}
// home the filter, slot 0
else if (!strcasecmp(mycmd, "PH"))
{
isMoving = true;
findindex();
currentslotnumber = 0;
isMoving = true;
}
// get current slot number
else if (!strcasecmp( mycmd, "GS"))
{
Serial.print("S");
Serial.print(currentslotnumber);
Serial.print("#");
}
// move to slot position
else if (!strcasecmp( mycmd, "MS"))
{
// get the slot number to move to
pos = decstr2long(param);
movetoslotnumber( pos); // move to the slot number
writenow = true; // updating of EEPROM ON
previousMillis = millis(); // start time interval
}
// assign filter color to slot position
else if (!strcasecmp(mycmd, "AF"))
{
// there are two arguments, slotnumber, filtercolor
// get slotnumber (range is 0 to 4)
int slotnumber;
pos = decstr2long(param);
slotnumber = pos;
// now get filtercolor, 1=R, 2=G, B=3, L=4, O=5
int colorfilter;
pos = decstr2long(param);
colorfilter = pos;
// check for valid slot number
if( (slotnumber >= 0) && (slotnumber <= 4))
{
// check that colorfilter is in range 1-5
if( (colorfilter >=1) && (colorfilter <=5) )
{
// assign the color filter to the slot number
myfilter.slots[slotnumber] = colorfilter;
}
else
{
// colorfilter is outside range
// do nothing
}
}
else
{
// slotnumber is outside range
// do nothing
}
}
// troubleshooting only - print currentaddr value, use in serial monitor mode is best
else if (!strcasecmp(mycmd, "XY"))
{
Serial.println();
Serial.print("Current Slot number=");
Serial.println(currentslotnumber);
Serial.print("maxsteps=");
Serial.println(maxsteps);
Serial.print("Slot[0]=");
switch(myfilter.slots[0])
{
case 1: // RED
Serial.println("RED");
break;
case 2: // GREEN
Serial.println("GREEN");
break;
case 3: // BLUE
Serial.println("BLUE");
case 4: // LUMIN
Serial.println("LUMIN");
case 5: // OTHER
Serial.println("OTHER");
default: // INVALID
Serial.println("INVALID SLOT NUMBER");
}
Serial.print("Slot[1]=");
switch(myfilter.slots[1])
{
case 1: // RED
Serial.println("RED");
break;
case 2: // GREEN
Serial.println("GREEN");
break;
case 3: // BLUE
Serial.println("BLUE");
case 4: // LUMIN
Serial.println("LUMIN");
case 5: // OTHER
Serial.println("OTHER");
default: // INVALID
Serial.println("INVALID SLOT NUMBER");
}
Serial.print("Slot[2]=");
switch(myfilter.slots[2])
{
case 1: // RED
Serial.println("RED");
break;
case 2: // GREEN
Serial.println("GREEN");
break;
case 3: // BLUE
Serial.println("BLUE");
case 4: // LUMIN
Serial.println("LUMIN");
case 5: // OTHER
Serial.println("OTHER");
default: // INVALID
Serial.println("INVALID SLOT NUMBER");
}
Serial.print("Slot[3]=");
switch(myfilter.slots[3])
{
case 1: // RED
Serial.println("RED");
break;
case 2: // GREEN
Serial.println("GREEN");
break;
case 3: // BLUE
Serial.println("BLUE");
case 4: // LUMIN
Serial.println("LUMIN");
case 5: // OTHER
Serial.println("OTHER");
default: // INVALID
Serial.println("INVALID SLOT NUMBER");
}
Serial.print("Slot[4]=");
switch(myfilter.slots[4])
{
case 1: // RED
Serial.println("RED");
break;
case 2: // GREEN
Serial.println("GREEN");
break;
case 3: // BLUE
Serial.println("BLUE");
case 4: // LUMIN
Serial.println("LUMIN");
case 5: // OTHER
Serial.println("OTHER");
default: // INVALID
Serial.println("INVALID SLOT NUMBER");
}
Serial.print("#");
}
// troubleshooting only - reset focuser defaults
else if (!strcasecmp(mycmd, "XZ"))
{
currentslotnumber = 0;
targetslotnumber = 0;
// setup the slots table
myfilter.validdata = 99;
myfilter.steps[0] = 0;
myfilter.steps[1] = 1000;
myfilter.steps[2] = 2000;
myfilter.steps[3] = -2000;
myfilter.steps[4] = -1000;
myfilter.slots[0] = FRED;
myfilter.slots[1] = FGREEN;
myfilter.slots[2] = FBLUE;
myfilter.slots[3] = FLUMIN;
myfilter.slots[4] = FOTHER;
// now write the data to EEPROM
EEPROM_writeAnything(currentaddr, myfilter); // update values in EEPROM
// Set focuser defaults.
findindex();
}
}
// convert string to long int
long decstr2long(char *line)
{
long ret = 0;
ret = strtol(line, NULL, 10);
return (ret);
}
// Setup
void setup()
{
// initialize serial for ASCOM
Serial.begin(9600);
eoc = 0;
idx = 0;
isMoving = false;
memset(line, 0, MAXCOMMAND);
mystepper.setSpeed(motorSpeed); // medium speed
currentaddr = 0; // start at 0 if not found later
found = false;
writenow = false;
datasize = sizeof( myfilter ); // should be 14 bytes
nlocations = EEPROMSIZE / datasize; // for AT328P = 1024 / datasize = 73 locations
for (int lp1 = 0; lp1 < nlocations; lp1++ )
{
int addr = lp1 * datasize;
EEPROM_readAnything( addr, myfilter );
// check to see if the data is valid
if ( myfilter.validdata == 99 )
{
// data was erased so write some default values
currentaddr = addr;
found = true;
}
}
if ( found == true )
{
// set the filter back to the previous settings
EEPROM_readAnything( currentaddr, myfilter );
myfilter.validdata = 0;
EEPROM_writeAnything(currentaddr, myfilter); // update values in EEPROM
// goto next free address and write data
currentaddr += datasize;
// bound check the eeprom storage and if greater than last index [0-EEPROMSIZE-1] then set to 0
if ( currentaddr >= (nlocations * datasize) ) currentaddr = 0;
myfilter.validdata = 99;
EEPROM_writeAnything(currentaddr, myfilter); // update values in EEPROM
}
else
{
// set defaults because not found
myfilter.validdata = 99;
myfilter.steps[0] = 0;
myfilter.steps[1] = 1000;
myfilter.steps[2] = 2000;
myfilter.steps[3] = -2000;
myfilter.steps[4] = -1000;
myfilter.slots[0] = FRED;
myfilter.slots[1] = FGREEN;
myfilter.slots[2] = FBLUE;
myfilter.slots[3] = FLUMIN;
myfilter.slots[4] = FOTHER;
// now write the data to EEPROM
EEPROM_writeAnything(currentaddr, myfilter); // update values in EEPROM
}
findindex();
}
// Main Loop
void loop()
{
// process the command string when a hash arrives:
if (eoc)
{
processCommand(line);
memset(line, 0, MAXCOMMAND);
eoc = false;
}
}
// SerialEvent occurs whenever new data comes in the serial RX.
void serialEvent()
{
// : starts the command, # ends the command, do not store these in the command buffer
// read the command until the terminating # character
while (Serial.available() && !eoc)
{
inChar = Serial.read();
if (inChar != '#' && inChar != ':')
{
line[idx++] = inChar;
if (idx >= MAXCOMMAND)
{
idx = MAXCOMMAND - 1;
}
}
else
{
if (inChar == '#')
{
eoc = 1;
idx = 0;
}
}
}
} |
Partager