Tag Archives: Bytes

The Easy Way? Or The Hard Way?

adventuretime car

“8 bits in 1 byte”

Hmm, so I went over the type of commands that the arduino would have to process. The most important function would be the GOTO function. And how accurate it will be depends on how accurate our sensors are. Telescope Driver Version 1 will use the magnetic encoder, resolution of 5.27 arcminutes. Telescope Driver Version 2 might use the motor’s quadrature encoder, resolution of 6.1 arcseconds, potentially. But using the quad’ will require measuring the amount of backlash, and using the mag encoder is much simpler.

Rather than use double floating point degrees (which is not advisable on Arduino), or send an angular position separately as ‘degrees’ ‘arcminutes’ ‘arcseconds’ as a struct of integers and other, I’ll just send an angular position as ‘calculated arcminutes’. Basically, it’s double floating point degree multiplied by 60, to yield arcminutes. The quotient (the whole number portion) is sent as 2 bytes Int (arduino) representing ‘arcminutes’. And the magnetic encoder resolution is only 5.27 arcminutes anyways. This simplifies the math a little.

An alternative is to send position as a quantity of arcseconds, which would occupy at least 3 bytes, maybe 4. In the case of 4 bytes, it can be treated as a long (arduino) integer data type

So for Ver1, the goto byte command string will consist of 1 command byte, 2 bytes Int (arduino) that represent ‘arcminutes’ position for azimuth, 2 bytes Int (arduino) that represent ‘arcminutes’ for altitude, and 1 terminating byte of char ‘;’ – So 6 bytes total for one GOTO command.

THE ALTERNATIVE : THE EASY WAY
We only send 2-byte unsigned integer, which correspond to the magnetic encoder outputs (a number from 0 to 4095). The Arduino is left with only a few functions, with no need to calculate or convert to/from degrees or minutes or seconds. Conversions within the Telescope Driver will be easy and accurate, or as accurate can be with double floating point. But that’s not as challenging as the first method.

Although not updated with latest development, you can find my latest arduino telescope driver stuff Here

jazz dance

NO FLUFF!! JUST CODE!! EXCITED!!!

nofluffdog

Hello Folks!

My current Arduino sketch contains code that reads in bytes from Serial. The neat thing is that I made an instruction buffer. It’s defined to hold 8 instructions at the moment, but I can easily increase it to 64. This allows the Arduino to buffer 7 instructions, whether they be movement instructions or getstatus instructions, and execute them sequentially. The Arduino Serial buffer itself is only 63 characters, that means it will only hold 63 bytes before we start overflowing. Each instruction is 6 bytes long, including terminating character ‘;’. The SerialEvent() is capable of consuming the instructions on the serial buffer as quickly as they come. Eventually, we’ll have to keep track of ‘states’ of the telescope, so we can simultaneously read in instructions from serial, buffer them internally, execute movement instructions, do getstatus instructions, pause instructions, and break instructions if necessary, And what’s really neat is that I didn’t have to rely on a QueueList class, or too heavily on String class, meaning the sketch is currently only 4.2kb. With all the free space, we can add some math and functionality, where the Arduino can ‘learn’ about it’s own sensors and motors.

*Basically, the code is at a point where it can read in ‘;’ terminated byte-string-commands. It can store in memory up to 8 byte-string-commands, but it can be expanded to much more. The ASCOM telescope driver is capable of sending byte-string-commands. There may be problems later on regarding sending and interpreting only bytes.

**I apologize in advance for the wordwrapping and bad formatting. I’m not used to wordpress with code.

//David Harbottle
//May 03, 2014
//Motorized Telescope Project
//PCC Engineering Club

#define bufflen 8
#define codelen 16

//the queue works, but Arduino Serial buffer is still limited at 63 bytes
byte byteArray[bufflen][codelen] = { };

int stringCount = 0;  //counts the number of instructions
int i = 0;  //byeArray's 2nd index counter, used by SerialEvent
int current = 0; //byteArray's 1st index counter, current instruction to run
int nextIn = 0; //byteArray's 1st index counter, index of array to write the next instruction

void setup() {
 // put your setup code here, to run once:
  Serial.begin(19200);
  pinMode(13, OUTPUT);
  digitalWrite(13,LOW);

  delay(1000);

  Serial.println("start");
}

//round robin indexer
void currentPlus() {
  current = current + 1;
  if (current >= bufflen)
  current = 0;
}

//round robin indexer
void nextInPlus() {
  nextIn = nextIn + 1;
  if (nextIn >= bufflen)
  nextIn = 0;
}


void loop() {
  if (current!=nextIn)
  { 
    Serial.write("instructions left : ");
    Serial.print(stringCount);
    Serial.write('\t');
 
    switch (byteArray[current][0])
    {
      case 48:
        Serial.write("You sent a char '0'.\t");
        break;
      case 49:
        Serial.write("You sent a char '1'.\t");
        break;
      case 50:
        Serial.write("You sent a char '2'.\t");
        break;
      case 51:
        Serial.write("You sent a char '3'.\t");
        break;
      case 52:
        Serial.write("You sent a char '4'.\t");
        break;
      case 53:
        Serial.write("You sent a char '5'.\t");
       break;
      case 54:
        Serial.write("You sent a char '6'.\t");
        break;
      case 55:
        Serial.write("You sent a char '7'.\t");
        break;
      case 56:
        Serial.print("You sent a char '8'.\t");
        break;
      case 57:
        Serial.write("You sent a char '9'.\t");
        break;
      default:
        Serial.write("You sent a non-cmd : ");
        Serial.write(byteArray[current][0]);
        Serial.write('\t');
        break; 
      }
 
 //clear out the instruction code
      for (int j = 0; j < codelen; j++) byteArray[current][j] = 0;

      currentPlus();
      stringCount--;
 
      Serial.write(";\n");
    }
    delay(500);
}


void serialEvent()
{
  byte inByte;

 //keep adding instructions as long as there are bytes in serial available
 //and while there is at least one empty spot for an instruction in the buffer
 //this stringCount+1, because nextIn points to an empty spot in the buffer
  while (Serial.available() && ((stringCount+1) < bufflen)){

    inByte = Serial.read();

    if (inByte == ';') {
      nextInPlus();
      stringCount++;
      i = 0;
    }
    else {
      byteArray[nextIn][i++] = inByte;
    }
  }
}

Additionally, I found a way to customize the color scheme of the Arduino IDE, Dark Arduino IDE by Jeff Thompson. It’s easier on the eyes.

newbackgroundide

We are getting closer to a working telescope driver!!!! So here’s an exciting gif!

tumblr_mkt2xxdOok1r72ht7o1_500

And remember guys :“Say No To Fluff”

no Marshmallow_fluff

-david