EnglishSvenska

Webbappar på Mac

Vissa webbplatser är mer som applikationer exempelvis Gmail eller Google Calendar. Säg att du vill ha dem vanliga appar med ikon och liggandes i program-mappen. I Windows kan detta enkelt göras från Chrome genom att klicka på Skapa programgenvägar men i OSX är det tyvärr inte lika smidigt men det går att lösa med Fluid app. Jag kör just nu WordPress som en webapp när jag skriver detta inlägg.

 

Tagged with:

Arduino-bibliotek för motordrivare L29x

l298n arduino library
Jag håller på att bygga om min självbalanserande robot och behövde då ett smidigt sätt att styra motordrivaren L298n. Eftersom jag styr roboten med PID-regulatorer ville jag att mitt bibliotek ska ta flyttal som både är negativa och positiva. Resultatet blev ett motordrivar-bibliotek som är väldigt enkelt att använda:

/* This example shows how to set motor speed in percentage where a negative
   value will run the motor backwards.
   This is very usuful in control applications. For example the output
   from a PID controller is most often a float and can be integrated with
   this library easily. 
*/
#include <L29x.h>
L29x motorLeft(9, 22, 23); // enable (PWM), motor pin 1, motor pin 2
L29x motorRight(8, 24, 29); // enable (PWM), motor pin 1, motor pin 2

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  motorLeft.setSpeedPercentage(-12.34);
  motorRight.setSpeedPercentage(-10);      
  delay(3000);
  motorLeft.setSpeedPercentage(100);
  motorRight.setSpeedPercentage(90);      
  delay(3000);
}
Tagged with:

Ersättare för EEPROM på Arduino Due

När jag fick min Arduino Due upptäckte jag att den inte har EEPROM vilket kan användas för att spara icke-flyktig data. Vanligtvis på en mikroprocessor så arbetar man med RAM-minnet vilket fungerar bra så länge man inte vill spara data mellan gångerna som processorn startar. Om man vill spara en variabel som ska vara kvar även när processorn förlorar ström behöver man en annan lösning där den vanligaste är att spara ner till EEPROM. På Arduino Due finns tyvärr inte detta minne men den har däremot ett riktigt stort flash-minne som kan konfigureras till att fungera på ett liknande sätt. Jag skrev därför ett bibliotek som gör just detta. Det är tänkt att kunna användas som en rak ersättare till EEPROM. DueFlashStorage är publicerad på Github.
Exempel:

// write the value 123 to address 0
dueFlashStorage.write(0,123);

// read byte at address 0
byte b = dueFlashStorage.read(0);

Eller hela exempelkoden:

/* This example will write 3 bytes to 3 different addresses and print them to the serial monitor.
Try resetting the Arduino Due or unplug the power to it. The values will stay stored. */

#include DueFlashStorage dueFlashStorage;

void setup() {
Serial.begin(115200);
byte b1 = 3;
uint8_t b2 = 1;
dueFlashStorage.write(0,b1);
dueFlashStorage.write(1,b2);
//dueFlashStorage.write(2,b2);
}

void loop() {
  /* read from flash at address 0 and 1 and print them */
  Serial.print("0:");
  Serial.print(dueFlashStorage.read(0));
  Serial.print(" 1:");
  Serial.print(dueFlashStorage.read(1));  

  /* read from address 2, increment it, print and then write incremented value back to flash storage */
  uint8_t i = dueFlashStorage.read(2)+1;
  Serial.print(" 2:");
  Serial.print(dueFlashStorage.read(2)); 
  dueFlashStorage.write(2,i);

  Serial.println();
  delay(1000);
}

Serial monitor:
arduino due flash example serial monitor 1

Men säg att man vill spara undan en hel konfiguration och inte bara några få bytes. Jag skrev biblioteket för att kunna hantera detta också:

// say you want to store a struct with parameters:
struct Configuration {
  uint8_t a;
  uint8_t b;
  int32_t bigInteger;
  char* message;
  char c;
};
Configuration configuration;

/* then write it to flash like this: */
byte b2[sizeof(Configuration)]; // create byte array to store the struct
memcpy(b2, &configuration, sizeof(Configuration)); // copy the struct to the byte array
dueFlashStorage.write(4, b2, sizeof(Configuration)); // write byte array to flash at address 4

/* and read from flash like this: */
byte* b = dueFlashStorage.readAddress(4); // byte array which is read from flash at adress 4
Configuration configurationFromFlash; // create a temporary struct
memcpy(&configurationFromFlash, b, sizeof(Configuration)); // copy byte array to temporary struct

Se hela koden på Github för ett fungerande exempel.

Tagged with:

Stockholm Robot Championship

Sthlm Robot Championship är ett öppet mästerskap för autonoma robotar med multipla tävlingsgrenar. Jag valde att ställa upp i fristil med min ombyggda självbalanserande robot.

(Foto: tim.gremalm.se)