hadie/hadie.c

160 wiersze
3.7 KiB
C
Czysty Zwykły widok Historia

/* hadie - High Altitude Balloon flight software */
/*============================================================*/
/* Copyright (C)2010 Philip Heron <phil@sanslogic.co.uk> */
/* */
/* This program is distributed under the terms of the GNU */
/* General Public License, version 2. You may use, modify, */
/* and redistribute it under the terms of this license. A */
/* copy should be included with this source. */
2010-06-07 12:10:32 +00:00
#include "config.h"
2010-07-06 11:16:42 +00:00
#include <stdio.h>
#include <string.h>
2010-06-08 10:45:12 +00:00
#include <util/delay.h>
2010-06-09 15:32:22 +00:00
#include <util/crc16.h>
#include <avr/interrupt.h>
2010-06-08 10:19:37 +00:00
#include "rtty.h"
2010-08-03 22:42:15 +00:00
#include "gps.h"
2010-07-06 11:16:42 +00:00
#include "c328.h"
#include "rs8.h"
2010-07-06 11:16:42 +00:00
/* Message buffer */
#define MSG_SIZE (100)
char msg[MSG_SIZE];
2010-08-03 22:42:15 +00:00
#define PREFIX "$$$$"
2010-07-06 11:16:42 +00:00
/* Image TX data */
#define PKT_SIZE (0x100)
2010-08-03 22:42:15 +00:00
#define PKT_SIZE_HEADER (0x0A)
2010-07-06 11:16:42 +00:00
#define PKT_SIZE_RSCODES (0x20)
#define PKT_SIZE_PAYLOAD (PKT_SIZE - PKT_SIZE_HEADER - PKT_SIZE_RSCODES)
uint8_t pkt[PKT_SIZE];
uint8_t image_pkts;
2010-07-06 11:16:42 +00:00
2010-08-03 22:42:15 +00:00
void init_packet(uint8_t *packet, uint8_t imageid, uint8_t pktid, uint8_t pkts, uint16_t width, uint16_t height)
2010-07-06 11:16:42 +00:00
{
2010-08-03 22:42:15 +00:00
packet[0] = 0x55; /* Sync */
packet[1] = 0x66; /* Type */
packet[2] = imageid; /* Image ID */
packet[3] = pktid; /* Packet ID */
packet[4] = pkts; /* Packets */
packet[5] = width >> 3; /* Width MCU */
packet[6] = height >> 3; /* Height MCU */
packet[7] = 0xFF; /* Next MCU offset */
packet[8] = 0x00; /* MCU ID MSB */
packet[9] = 0x00; /* MCU ID LSB */
2010-07-06 11:16:42 +00:00
memset(&packet[PKT_SIZE_HEADER], 0, PKT_SIZE_PAYLOAD);
}
char tx_image(void)
2010-06-09 15:32:22 +00:00
{
2010-07-06 11:16:42 +00:00
static char setup = 0;
static uint8_t img_id = 0;
static uint8_t pkt_id;
2010-06-09 15:32:22 +00:00
2010-07-06 11:16:42 +00:00
if(!setup)
{
2010-08-07 15:58:06 +00:00
uint16_t image_len;
2010-07-06 11:16:42 +00:00
2010-08-07 15:58:06 +00:00
if(c3_open(SR_320x240) != 0) return(setup);
setup = -1;
2010-07-06 11:16:42 +00:00
pkt_id = 0;
img_id++;
2010-08-07 15:58:06 +00:00
/* Calculate the number of packets needed for this image */
image_len = c3_filesize();
image_pkts = image_len / PKT_SIZE_PAYLOAD;
image_pkts += (image_len % PKT_SIZE_PAYLOAD > 0 ? 1 : 0);
2010-07-06 11:16:42 +00:00
}
2010-08-07 15:58:06 +00:00
/* Initialise the packet */
init_packet(pkt, img_id, pkt_id++, image_pkts, 320, 240);
2010-06-09 15:32:22 +00:00
2010-08-07 15:58:06 +00:00
c3_read(&pkt[PKT_SIZE_HEADER], PKT_SIZE_PAYLOAD);
if(c3_eof())
2010-07-06 11:16:42 +00:00
{
2010-08-07 15:58:06 +00:00
c3_close();
setup = 0;
2010-07-06 11:16:42 +00:00
}
encode_rs_8(&pkt[1], &pkt[PKT_SIZE_HEADER + PKT_SIZE_PAYLOAD], 0);
2010-08-03 22:42:15 +00:00
rtx_string_P(PSTR("UUU")); /* U = 0x55 */
2010-07-06 11:16:42 +00:00
rtx_data(pkt, PKT_SIZE);
2010-08-03 22:42:15 +00:00
return(setup);
}
uint16_t crccat(char *msg)
{
uint16_t x;
for(x = 0xFFFF; *msg; msg++)
x = _crc_xmodem_update(x, *msg);
snprintf(msg, 8, "*%04X\n", x);
return(x);
}
char tx_telemetry(void)
{
static unsigned int counter = 0;
gpsfix_t gps;
/* Read the GPS data */
gps_parse(&gps);
rtx_wait();
snprintf(msg, MSG_SIZE,
PREFIX CALLSIGN ",%u,%02i:%02i:%02i,%s%i.%06lu,%s%i.%06lu,%li,%i:%i",
2010-08-03 22:42:15 +00:00
counter++,
gps.hour, gps.minute, gps.second,
(gps.latitude_h == 'S' ? "-" : ""),
2010-08-03 22:42:15 +00:00
gps.latitude_i, gps.latitude_f,
(gps.longitude_h == 'W' ? "-" : ""),
2010-08-03 22:42:15 +00:00
gps.longitude_i, gps.longitude_f,
gps.altitude, gps.fix, gps.sats);
/* Append the checksum, skipping the first four $'s */
crccat(msg + 4);
/* Begin transmitting */
rtx_string(msg);
2010-07-06 11:16:42 +00:00
return(0);
2010-06-09 15:32:22 +00:00
}
int main(void)
{
2010-08-03 22:42:15 +00:00
char r;
2010-07-06 11:16:42 +00:00
/* Initalise the various bits */
2010-06-08 10:19:37 +00:00
rtx_init();
2010-08-03 22:42:15 +00:00
gps_init();
2010-07-06 11:16:42 +00:00
c3_init();
/* Let the radio settle before beginning */
2010-06-08 10:45:12 +00:00
_delay_ms(2000);
2010-07-06 11:16:42 +00:00
/* Start interrupts and enter the main loop */
sei();
2010-06-08 10:45:12 +00:00
while(1)
{
2010-08-03 22:49:44 +00:00
r = 10;
2010-08-03 22:42:15 +00:00
if(tx_image() == -1)
{
/* The camera goes to sleep while transmitting telemetry,
* sync'ing here seems to prevent it. */
c3_sync();
r = 1;
}
rtx_string_P(PSTR("\n"));
for(; r > 0; r--) tx_telemetry();
}
return(0);
}