Small little update
Its been over a week since I last posted but I haven’t been slacking off! Instead, I’ve been seriously thinking through and writing the code which will hopefully take LOCARB to Hawaii.
My code is pretty straight forward, with global variables holding the sensor data, and functions all written out for grabbing that data. The main loop is actually just function calls. One of the hurdles I had to overcome was writing the functions in a way which were non-blocking; code which would allow other operations to run while waiting for the previous function to finish. Arduinos aren’t multitasking so they just run through a loop from start to finish and any code which use delays or for/while loops will block the other code from running until it is finished. I got pretty familiar with millis() as it allows functions to run by checking the time instead of pausing the execution of code using delay().
The waiting game…
I was also able to decide on which rudder servo I would go with. I contacted KST servos through Alibaba and was able to purchase two servos which had both contact-less position sensors and brush-less motors. They are the KST models MS-815 and MS-825. Once I get them, ill be able to test power consumption and performance with the Pixhawk. If everything checks out with the power consumption, ill be happy to have a servo that has metal gears and components which should be able to withstand the number of mechanical actuations for a roundtrip from San Francisco to Hawaii.
I also received my Rockblock Naked MK2 modem from Sparkfun, but am waiting on the 5v – 3v logic shifter so I can connect it to my Arduino (Arduino uses 5v TTL, Sat modem 3v). While waiting for that to arrive, I worked on the function which will format the data to be sent by the Rockblock modem. At the moment I have an sprintf function which sends out a string text message around 80 bytes long. Since Rockseven charges 1 credit per 50 bytes of short burst data, it will probably cost ~.20c per message. Trying to keep running costs low, I added a way to change the messaging interval on LOCARB while its at sea. I figure ill want to track LOCARB with a little more frequency when its closer to a body of land than when it is in the middle of the ocean.
So at this time, i’m waiting on my KST servos and logic level shifters to come in the mail. After I receive them, ill be able to focus on the main send/receive functions using the Rockblock modem. I’m pretty excited about the progress I’ve made with the electronics portion of LOCARB, as satellite communications comprise around the last 20% of the job. Once that’s done, I can then move to building LOCARB’s hull and finalizing/building the power system with real power consumption figures from the sat comm modem and rudder servo.
A little about formatting a message…
In case you wanted to see how sprintf works on the Arduino, I’ve included a test sketch which shows how I formatted the message. One thing to note is that sprintf doesnt work well with float variables on the Arduino. However, there is a function which handles converting a float variable to a char array which allows you to easily incorporate floats into a string buffer.
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 |
int gps_Fix = 4; //gps fix int gps_Vel = 1; //gps groundspeed int gps_Head = 250; //gps heading int sys_Bat = 1254; //battery voltage int sys_Bcur = 1; //battery current int sys_Brem = 99; // int leaksensor_Value = 1; //leak sensor reading int capsized = 71; //how many times capsized int px4_Resetcounter = 41; // how many times px4 restarted int motor_Counter = 12; //how many times motor stalled int dht1_hum = 66; //Stores humidity value int dht1_temp = 67; //Stores temperature value int dht2_hum = 68; //Stores humidity value int dht2_temp = 69; //Stores temperature value int rpm = 2300; //motor rpm int motor_Temperature = 120; //motor temp int rudder_Temperature = 109; //rudder temp float gps_Lat = 310529340; //float from mavlink - gps lat float gps_Long = -1620703125; //float from mavlink - gps long float gps_Latfloated = gps_Lat / 10000000; //convert mavlink to format for WebDB float gps_Longfloated = gps_Long / 10000000; //convert mavlink to format for WebDB char buff_Lat[15]; //character array buffer for dtostrf gps_Lat char buff_Long[15]; //character array buffer for dtostrf gps_Long void setup() { // put your setup code here, to run once: Serial.begin(9600); dtostrf(gps_Latfloated, 11, 7, buff_Lat); //(90 or -90 lat) 11 is the number of characters total, 7 characters after the decimal dtostrf(gps_Longfloated, 12, 7, buff_Long); //(180 or -180 long) 12 is the number of characters total, 7 characters after the decimal Serial.println("Beginning to talk to the RockBLOCK..."); char outBuffer[80]; // Always try to keep message short sprintf(outBuffer, "%d,%02d,%003d,%0004d,%d,%02d,%d,%02d,%02d,%02d,%3d,%3d,%3d,%3d,%3d,%3d,%4d,%s,%s", gps_Fix, gps_Vel, gps_Head, sys_Bat, sys_Bcur, sys_Brem, leaksensor_Value, capsized, px4_Resetcounter, motor_Counter, dht1_hum, dht1_temp, dht2_hum, dht2_temp, motor_Temperature, rudder_Temperature, rpm, buff_Lat, buff_Long); Serial.println("Formatted Test message:"); Serial.print("'"); Serial.print(outBuffer); Serial.print("'"); } void loop() { // put your main code here, to run repeatedly: } |
The dtostrf() function easily takes a float variable and formats it into a char array for use with a string buffer. Its nice and easy!
Here is the console output of the above code. I know many of you are probably wondering why i’m formatting the GPS coordinates with a decimal now, rather than later…This is something I still haven’t decided yet, but I figure since i’m sending 80 bytes anyways, i might as well take up the limit of 100 bytes before the next credit is charged. This way I wont need to write additional code on the web server which will be receiving this data to properly format the GPS coordinates. I just realized I need to do the same thing with my battery voltage as well… This is something that I could very well handle on the server side, so its up for revision as I begin to work with the sat comm modem.
1 2 3 |
Beginning to talk to the RockBLOCK... Formatted Test message: '4,01,250,1254,1,99,1,71,41,12, 66, 67, 68, 69,120,109,2300, 31.0529350,-162.0703100' |
If you want to read some resources that helped me on this part of the process, check out these links: