Update main.c

pull/26/head
alanbjohnston 7 years ago committed by GitHub
parent e3a7560ff0
commit 1bcbeac093
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -80,8 +80,8 @@ int lower_digit(int number);
#define BUF_SIZE (S_RATE*10) /* 2 second buffer */ #define BUF_SIZE (S_RATE*10) /* 2 second buffer */
/* /*
// BPSK Settings // BPSK Settings
#define BIT_RATE 1200 // 200 for DUV #define BIT_RATE 1200 // 200 for DUV
#define FSK 0 // 1 for DUV #define FSK 0 // 1 for DUV
#define RS_FRAMES 3 // 3 frames for BPSK, 1 for DUV #define RS_FRAMES 3 // 3 frames for BPSK, 1 for DUV
#define PAYLOADS 6 // 1 for DUV #define PAYLOADS 6 // 1 for DUV
#define DATA_LEN 78 // 56 for DUV #define DATA_LEN 78 // 56 for DUV
@ -92,7 +92,7 @@ int lower_digit(int number);
*/ */
// FSK Settings // FSK Settings
#define BIT_RATE 200 #define BIT_RATE 200
#define FSK 1 #define FSK 1
#define RS_FRAMES 1 #define RS_FRAMES 1
#define PAYLOADS 1 #define PAYLOADS 1
#define RS_FRAME_LEN 64 #define RS_FRAME_LEN 64
@ -111,10 +111,16 @@ int smaller;
int flip_ctr = 0; int flip_ctr = 0;
int phase = 1; int phase = 1;
int ctr = 0; int ctr = 0;
int sock = 0;
void write_to_buffer(int i, int symbol, int val); void write_to_buffer(int i, int symbol, int val);
void write_wave(); void write_wave();
#define SAMPLES (S_RATE / BIT_RATE) #define SAMPLES (S_RATE / BIT_RATE)
<<<<<<< HEAD
#define FRAME_CNT 60// 11 //33 // Add 3 frames to the count #define FRAME_CNT 60// 11 //33 // Add 3 frames to the count
=======
#define FRAME_CNT 11// //33 // Add 3 frames to the count
>>>>>>> 363442c3e93a3058a0bb88473dc4fa192b6425ad
//#define BUF_LEN (FRAME_CNT * (SYNC_BITS + 10 * (8 + 6 * DATA_LEN + 96)) * SAMPLES) //#define BUF_LEN (FRAME_CNT * (SYNC_BITS + 10 * (8 + 6 * DATA_LEN + 96)) * SAMPLES)
#define BUF_LEN (FRAME_CNT * (SYNC_BITS + 10 * (HEADER_LEN + RS_FRAMES * (RS_FRAME_LEN + PARITY_LEN))) * SAMPLES) #define BUF_LEN (FRAME_CNT * (SYNC_BITS + 10 * (HEADER_LEN + RS_FRAMES * (RS_FRAME_LEN + PARITY_LEN))) * SAMPLES)
@ -194,9 +200,9 @@ struct SensorConfig config_sensor(char *bus, int address, int milliAmps) {
struct SensorConfig data; struct SensorConfig data;
if (access(bus, W_OK | R_OK) < 0) { // Test if I2C Bus is missing if (access(bus, W_OK | R_OK) < 0) { // Test if I2C Bus is missing
printf("ERROR: %s bus not present \n", bus); printf("ERROR: %s bus not present \n", bus);
data.fd = OFF; data.fd = OFF;
return (data); return (data);
} }
data.fd = wiringPiI2CSetupInterface(bus, address); data.fd = wiringPiI2CSetupInterface(bus, address);
@ -207,7 +213,7 @@ struct SensorConfig config_sensor(char *bus, int address, int milliAmps) {
INA219_CONFIG_SADCRES_12BIT_1S_532US | INA219_CONFIG_SADCRES_12BIT_1S_532US |
INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS; INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS;
if (milliAmps == 400) { // INA219 16V 400mA configuration if (milliAmps == 400) { // INA219 16V 400mA configuration
data.calValue = 8192; data.calValue = 8192;
data.powerMultiplier = 1; data.powerMultiplier = 1;
data.currentDivider = 20; // 40; in Adafruit config data.currentDivider = 20; // 40; in Adafruit config
@ -219,8 +225,8 @@ struct SensorConfig config_sensor(char *bus, int address, int milliAmps) {
} }
#ifdef DEBUG_LOGGING #ifdef DEBUG_LOGGING
printf("Sensor %s %x configuration: %d %d %d %d %d\n", bus, address, data.fd, printf("Sensor %s %x configuration: %d %d %d %d %d\n", bus, address, data.fd,
data.config, data.calValue, data.currentDivider, data.powerMultiplier); data.config, data.calValue, data.currentDivider, data.powerMultiplier);
#endif #endif
return data; return data;
} }
@ -235,7 +241,7 @@ char dest_addr[5] = "CQ";
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
if (argc > 1) { if (argc > 1) {
strcpy(src_addr, argv[1]); strcpy(src_addr, argv[1]);
} }
wiringPiSetup (); wiringPiSetup ();
@ -250,9 +256,9 @@ int main(int argc, char *argv[]) {
{ {
printf("Creating config file."); printf("Creating config file.");
config_file = fopen("sim.cfg","w"); config_file = fopen("sim.cfg","w");
fprintf(config_file, "%s %d", "KU2Y", 100); fprintf(config_file, "%s %d", "KU2Y", 100);
fclose(config_file); fclose(config_file);
config_file = fopen("sim.cfg","r"); config_file = fopen("sim.cfg","r");
} }
char* cfg_buf[100]; char* cfg_buf[100];
@ -289,6 +295,37 @@ int main(int argc, char *argv[]) {
AX25_PREAMBLE_LEN, AX25_PREAMBLE_LEN,
AX25_POSTAMBLE_LEN); AX25_POSTAMBLE_LEN);
// socket open
int error = 0;
struct sockaddr_in address;
int valread;
struct sockaddr_in serv_addr;
// char *hello = "Hello from client";
// char buffer[1024] = {0};
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Socket creation error \n");
error = 1;
}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
{
printf("\nInvalid address/ Address not supported \n");
error = 1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\nConnection Failed \n");
error = 1;
}
/* Infinite loop */ /* Infinite loop */
//for (;;) //for (;;)
@ -316,9 +353,9 @@ int main(int argc, char *argv[]) {
char cmdbuffer[1000]; char cmdbuffer[1000];
FILE* transmit; FILE* transmit;
if (FSK == 1) { if (FSK == 1) {
transmit = popen("sudo cat /home/pi/CubeSatSim/transmit.wav | csdr convert_i16_f | csdr gain_ff 7000 | csdr convert_f_samplerf 20833 | sudo /home/pi/CubeSatSim/rpitx/rpitx -i- -m RF -f 434.9e3 2>&1", "r"); transmit = popen("sudo cat /home/pi/CubeSatSim/transmit.wav | csdr convert_i16_f | csdr gain_ff 7000 | csdr convert_f_samplerf 20833 | sudo /home/pi/CubeSatSim/rpitx/rpitx -i- -m RF -f 434.9e3 2>&1", "r");
} else { } else {
transmit = popen("sudo cat /home/pi/CubeSatSim/transmit.wav | csdr convert_i16_f | csdr fir_interpolate_cc 2 | csdr dsb_fc | csdr bandpass_fir_fft_cc 0.002 0.06 0.01 | csdr fastagc_ff | sudo /home/pi/CubeSatSim/rpitx/sendiq -i /dev/stdin -s 96000 -f 434.9e6 -t float 2>&1", "r"); transmit = popen("sudo cat /home/pi/CubeSatSim/transmit.wav | csdr convert_i16_f | csdr fir_interpolate_cc 2 | csdr dsb_fc | csdr bandpass_fir_fft_cc 0.002 0.06 0.01 | csdr fastagc_ff | sudo /home/pi/CubeSatSim/rpitx/sendiq -i /dev/stdin -s 96000 -f 434.9e6 -t float 2>&1", "r");
} }
fgets(cmdbuffer, 1000, transmit); fgets(cmdbuffer, 1000, transmit);
pclose(transmit); pclose(transmit);
@ -326,7 +363,7 @@ int main(int argc, char *argv[]) {
*/ */
// printf("%s \n", b); // printf("%s \n", b);
/* /*
digitalWrite (0, LOW); digitalWrite (0, LOW);
@ -375,24 +412,24 @@ static void init_rf() {
// //
int lower_digit(int number) { int lower_digit(int number) {
int digit = 0; int digit = 0;
if (number < 100) if (number < 100)
digit = number - ((int)(number/10) * 10); digit = number - ((int)(number/10) * 10);
else else
fprintf(stderr,"ERROR: Not a digit in lower_digit!\n"); fprintf(stderr,"ERROR: Not a digit in lower_digit!\n");
return digit; return digit;
} }
// Returns upper digit of a number which must be less than 99 // Returns upper digit of a number which must be less than 99
// //
int upper_digit(int number) { int upper_digit(int number) {
int digit = 0; int digit = 0;
if (number < 100) if (number < 100)
digit = (int)(number/10); digit = (int)(number/10);
else else
fprintf(stderr,"ERROR: Not a digit in upper_digit!\n"); fprintf(stderr,"ERROR: Not a digit in upper_digit!\n");
return digit; return digit;
} }
int get_tlm(char *str) { int get_tlm(char *str) {
@ -407,18 +444,18 @@ int get_tlm(char *str) {
reading[count] = read_sensor_data(sensor[count]); reading[count] = read_sensor_data(sensor[count]);
#ifdef DEBUG_LOGGING #ifdef DEBUG_LOGGING
printf("Read sensor[%d] % 4.2fV % 6.1fmA % 6.1fmW \n", printf("Read sensor[%d] % 4.2fV % 6.1fmA % 6.1fmW \n",
count, reading[count].voltage, reading[count].current, reading[count].power); count, reading[count].voltage, reading[count].current, reading[count].power);
#endif #endif
} }
tlm[1][A] = (int)(reading[BUS].voltage /15.0 + 0.5) % 100; // Current of 5V supply to Pi tlm[1][A] = (int)(reading[BUS].voltage /15.0 + 0.5) % 100; // Current of 5V supply to Pi
tlm[1][B] = (int) (99.5 - reading[PLUS_X].current/10.0) % 100; // +X current [4] tlm[1][B] = (int) (99.5 - reading[PLUS_X].current/10.0) % 100; // +X current [4]
tlm[1][C] = (int) (99.5 - reading[MINUS_X].current/10.0) % 100; // X- current [10] tlm[1][C] = (int) (99.5 - reading[MINUS_X].current/10.0) % 100; // X- current [10]
tlm[1][D] = (int) (99.5 - reading[PLUS_Y].current/10.0) % 100; // +Y current [7] tlm[1][D] = (int) (99.5 - reading[PLUS_Y].current/10.0) % 100; // +Y current [7]
tlm[2][A] = (int) (99.5 - reading[MINUS_Y].current/10.0) % 100; // -Y current [10] tlm[2][A] = (int) (99.5 - reading[MINUS_Y].current/10.0) % 100; // -Y current [10]
tlm[2][B] = (int) (99.5 - reading[PLUS_Z].current/10.0) % 100; // +Z current [10] // was 70/2m transponder power, AO-7 didn't have a Z panel tlm[2][B] = (int) (99.5 - reading[PLUS_Z].current/10.0) % 100; // +Z current [10] // was 70/2m transponder power, AO-7 didn't have a Z panel
tlm[2][C] = (int) (99.5 - reading[MINUS_Z].current/10.0) % 100; // -Z current (was timestamp) tlm[2][C] = (int) (99.5 - reading[MINUS_Z].current/10.0) % 100; // -Z current (was timestamp)
tlm[2][D] = (int)(50.5 + reading[BAT].current/10.0) % 100; // NiMH Battery current tlm[2][D] = (int)(50.5 + reading[BAT].current/10.0) % 100; // NiMH Battery current
tlm[3][A] = abs((int)((reading[BAT].voltage * 10.0) - 65.5) % 100); tlm[3][A] = abs((int)((reading[BAT].voltage * 10.0) - 65.5) % 100);
@ -439,16 +476,16 @@ int get_tlm(char *str) {
FILE *cpuTempSensor = fopen("/sys/class/thermal/thermal_zone0/temp", "r"); FILE *cpuTempSensor = fopen("/sys/class/thermal/thermal_zone0/temp", "r");
if (cpuTempSensor) { if (cpuTempSensor) {
double cpuTemp; double cpuTemp;
fscanf (cpuTempSensor, "%lf", &cpuTemp); fscanf (cpuTempSensor, "%lf", &cpuTemp);
cpuTemp /= 1000; cpuTemp /= 1000;
#ifdef DEBUG_LOGGING #ifdef DEBUG_LOGGING
printf("CPU Temp Read: %6.1f\n", cpuTemp); printf("CPU Temp Read: %6.1f\n", cpuTemp);
#endif #endif
tlm[4][B] = (int)((95.8 - cpuTemp)/1.48 + 0.5) % 100; tlm[4][B] = (int)((95.8 - cpuTemp)/1.48 + 0.5) % 100;
fclose (cpuTempSensor); fclose (cpuTempSensor);
} }
tlm[6][B] = 0 ; tlm[6][B] = 0 ;
@ -459,7 +496,7 @@ int get_tlm(char *str) {
int k, j; int k, j;
for (k = 1; k < 7; k++) { for (k = 1; k < 7; k++) {
for (j = 1; j < 5; j++) { for (j = 1; j < 5; j++) {
printf(" %2d ", tlm[k][j]); printf(" %2d ", tlm[k][j]);
} }
printf("\n"); printf("\n");
} }
@ -498,41 +535,41 @@ int get_tlm_fox() {
printf("Reset Count: %d Uptime since Reset: %ld \n", reset_count, uptime); printf("Reset Count: %d Uptime since Reset: %ld \n", reset_count, uptime);
fclose(uptime_file); fclose(uptime_file);
int i; int i;
long int sync = SYNC_WORD; long int sync = SYNC_WORD;
smaller = S_RATE/(2 * freq_Hz); smaller = S_RATE/(2 * freq_Hz);
/* /*
short int b[DATA_LEN] = {0x00,0x7E,0x03, short int b[DATA_LEN] = {0x00,0x7E,0x03,
0x00,0x00,0x00,0x00,0xE6,0x01,0x00,0x27,0xD1,0x02, 0x00,0x00,0x00,0x00,0xE6,0x01,0x00,0x27,0xD1,0x02,
0xE5,0x40,0x04,0x18,0xE1,0x04,0x00,0x00,0x00,0x00,0x00,0x00, 0xE5,0x40,0x04,0x18,0xE1,0x04,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x03,0x02,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x03,0x02,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
short int h[HEADER_LEN] = {0x05,0x00,0x00,0x00,0x00,0x10,0x00,0x00}; short int h[HEADER_LEN] = {0x05,0x00,0x00,0x00,0x00,0x10,0x00,0x00};
*/ */
short int b[DATA_LEN]; short int b[DATA_LEN];
memset(b, 0, sizeof(b)); memset(b, 0, sizeof(b));
short int h[HEADER_LEN]; short int h[HEADER_LEN];
memset(h, 0, sizeof(h)); memset(h, 0, sizeof(h));
short int b10[DATA_LEN], h10[HEADER_LEN]; short int b10[DATA_LEN], h10[HEADER_LEN];
short int rs_frame[RS_FRAMES][223]; short int rs_frame[RS_FRAMES][223];
unsigned char parities[RS_FRAMES][PARITY_LEN],inputByte; unsigned char parities[RS_FRAMES][PARITY_LEN],inputByte;
/* /*
int id = 7, frm_type = 0x01, TxTemp = 0, IHUcpuTemp = 0; int id = 7, frm_type = 0x01, TxTemp = 0, IHUcpuTemp = 0;
int batt_a_v = 0, batt_b_v = 0, batt_c_v = 8.95 * 100, battCurr = 48.6 * 10; int batt_a_v = 0, batt_b_v = 0, batt_c_v = 8.95 * 100, battCurr = 48.6 * 10;
int posXv = 296, negXv = 45, posYv = 220, negYv = 68, int posXv = 296, negXv = 45, posYv = 220, negYv = 68,
posZv = 280, negZv = 78; posZv = 280, negZv = 78;
*/ */
int id = 7, frm_type = 0x01, TxTemp = 0, IHUcpuTemp = 0; int id = 7, frm_type = 0x01, TxTemp = 0, IHUcpuTemp = 0;
int batt_a_v = 0, batt_b_v = 0, batt_c_v = 0, battCurr = 0; int batt_a_v = 0, batt_b_v = 0, batt_c_v = 0, battCurr = 0;
int posXv = 0, negXv = 0, posYv = 0, negYv = 0, int posXv = 0, negXv = 0, posYv = 0, negYv = 0,
posZv = 0, negZv = 0; posZv = 0, negZv = 0;
int head_offset = 0; int head_offset = 0;
for (int frames = 0; frames < FRAME_CNT; frames++) for (int frames = 0; frames < FRAME_CNT; frames++)
@ -543,7 +580,7 @@ int get_tlm_fox() {
reading[count] = read_sensor_data(sensor[count]); reading[count] = read_sensor_data(sensor[count]);
#ifdef DEBUG_LOGGING #ifdef DEBUG_LOGGING
printf("Read sensor[%d] % 4.2fV % 6.1fmA % 6.1fmW \n", printf("Read sensor[%d] % 4.2fV % 6.1fmA % 6.1fmW \n",
count, reading[count].voltage, reading[count].current, reading[count].power); count, reading[count].voltage, reading[count].current, reading[count].power);
#endif #endif
} }
/* /*
@ -563,9 +600,9 @@ int get_tlm_fox() {
*/ */
FILE *cpuTempSensor = fopen("/sys/class/thermal/thermal_zone0/temp", "r"); FILE *cpuTempSensor = fopen("/sys/class/thermal/thermal_zone0/temp", "r");
if (cpuTempSensor) { if (cpuTempSensor) {
double cpuTemp; double cpuTemp;
fscanf (cpuTempSensor, "%lf", &cpuTemp); fscanf (cpuTempSensor, "%lf", &cpuTemp);
cpuTemp /= 1000; cpuTemp /= 1000;
#ifdef DEBUG_LOGGING #ifdef DEBUG_LOGGING
printf("CPU Temp Read: %6.1f\n", cpuTemp); printf("CPU Temp Read: %6.1f\n", cpuTemp);
@ -621,141 +658,144 @@ int get_tlm_fox() {
encodeA(b, 18 + head_offset,negYv); encodeA(b, 18 + head_offset,negYv);
encodeB(b, 19 + head_offset,negZv); encodeB(b, 19 + head_offset,negZv);
/* batt_c_v += 10; /* batt_c_v += 10;
battCurr -= 10; battCurr -= 10;
encodeA(b, 3 + head_offset, batt_c_v); encodeA(b, 3 + head_offset, batt_c_v);
encodeA(b, 9 + head_offset, battCurr); encodeA(b, 9 + head_offset, battCurr);
*/ */
int ctr1 = 0; int ctr1 = 0;
int ctr3 = 0; int ctr3 = 0;
for (i = 0; i < RS_FRAME_LEN; i++) for (i = 0; i < RS_FRAME_LEN; i++)
{ {
for (int j = 0; j < RS_FRAMES ; j++) for (int j = 0; j < RS_FRAMES ; j++)
{ {
if (!((i == (RS_FRAME_LEN - 1)) && (j == 2))) // skip last one for BPSK if (!((i == (RS_FRAME_LEN - 1)) && (j == 2))) // skip last one for BPSK
{ {
if (ctr1 < HEADER_LEN) if (ctr1 < HEADER_LEN)
{ {
rs_frame[j][i] = h[ctr1]; rs_frame[j][i] = h[ctr1];
update_rs(parities[j], h[ctr1]); update_rs(parities[j], h[ctr1]);
// printf("header %d rs_frame[%d][%d] = %x \n", ctr1, j, i, h[ctr1]); // printf("header %d rs_frame[%d][%d] = %x \n", ctr1, j, i, h[ctr1]);
data8[ctr1++] = rs_frame[j][i]; data8[ctr1++] = rs_frame[j][i];
// printf ("data8[%d] = %x \n", ctr1 - 1, rs_frame[j][i]); // printf ("data8[%d] = %x \n", ctr1 - 1, rs_frame[j][i]);
} }
else else
{ {
rs_frame[j][i] = b[ctr3 % DATA_LEN]; rs_frame[j][i] = b[ctr3 % DATA_LEN];
update_rs(parities[j], b[ctr3 % DATA_LEN]); update_rs(parities[j], b[ctr3 % DATA_LEN]);
// printf("%d rs_frame[%d][%d] = %x %d \n", // printf("%d rs_frame[%d][%d] = %x %d \n",
// ctr1, j, i, b[ctr3 % DATA_LEN], ctr3 % DATA_LEN); // ctr1, j, i, b[ctr3 % DATA_LEN], ctr3 % DATA_LEN);
data8[ctr1++] = rs_frame[j][i]; data8[ctr1++] = rs_frame[j][i];
// printf ("data8[%d] = %x \n", ctr1 - 1, rs_frame[j][i]); // printf ("data8[%d] = %x \n", ctr1 - 1, rs_frame[j][i]);
ctr3++; ctr3++;
} }
} }
} }
} }
printf("Parities "); printf("Parities ");
for (int m = 0; m < PARITY_LEN; m++) { for (int m = 0; m < PARITY_LEN; m++) {
printf("%d ", parities[0][m]); printf("%d ", parities[0][m]);
} }
printf("\n"); printf("\n");
int ctr2 = 0; int ctr2 = 0;
memset(data10,0,sizeof(data10)); memset(data10,0,sizeof(data10));
int rd = 0; int rd = 0;
int nrd; int nrd;
for (i = 0; i < DATA_LEN * PAYLOADS + HEADER_LEN; i++) // 476 for BPSK for (i = 0; i < DATA_LEN * PAYLOADS + HEADER_LEN; i++) // 476 for BPSK
{ {
data10[ctr2] = (Encode_8b10b[rd][((int)data8[ctr2])] & 0x3ff); data10[ctr2] = (Encode_8b10b[rd][((int)data8[ctr2])] & 0x3ff);
nrd = (Encode_8b10b[rd][((int)data8[ctr2])] >> 10) & 1; nrd = (Encode_8b10b[rd][((int)data8[ctr2])] >> 10) & 1;
// printf ("data10[%d] = encoded data8[%d] = %x \n", // printf ("data10[%d] = encoded data8[%d] = %x \n",
// ctr2, ctr2, data10[ctr2]); // ctr2, ctr2, data10[ctr2]);
rd = nrd; // ^ nrd; rd = nrd; // ^ nrd;
ctr2++; ctr2++;
} }
for (i = 0; i < PARITY_LEN; i++) for (i = 0; i < PARITY_LEN; i++)
{ {
for (int j = 0; j < RS_FRAMES; j++) for (int j = 0; j < RS_FRAMES; j++)
{ {
data10[ctr2++] = (Encode_8b10b[rd][((int)parities[j][i])] & 0x3ff); data10[ctr2++] = (Encode_8b10b[rd][((int)parities[j][i])] & 0x3ff);
nrd = (Encode_8b10b[rd][((int)parities[j][i])] >> 10) & 1; nrd = (Encode_8b10b[rd][((int)parities[j][i])] >> 10) & 1;
// printf ("data10[%d] = encoded parities[%d][%d] = %x \n", // printf ("data10[%d] = encoded parities[%d][%d] = %x \n",
// ctr2 - 1, j, i, data10[ctr2 - 1]); // ctr2 - 1, j, i, data10[ctr2 - 1]);
rd = nrd; rd = nrd;
} }
} }
int data; int data;
int val; int val;
int offset = 0; int offset = 0;
ctr = 0;
for (i = 1; i <= SYNC_BITS * SAMPLES; i++) flip_ctr = 0;
{ phase = 1;
write_wave(ctr);
if ( (i % SAMPLES) == 0) { for (i = 1; i <= SYNC_BITS * SAMPLES; i++)
int bit = SYNC_BITS - i/SAMPLES + 1; {
val = sync; write_wave(ctr);
data = val & 1 << (bit - 1); if ( (i % SAMPLES) == 0) {
// printf ("%d i: %d new frame %d sync bit %d = %d \n", int bit = SYNC_BITS - i/SAMPLES + 1;
// ctr/SAMPLES, i, frames, bit, (data > 0) ); val = sync;
if (FSK) data = val & 1 << (bit - 1);
{ // printf ("%d i: %d new frame %d sync bit %d = %d \n",
phase = ((data != 0) * 2) - 1; // ctr/SAMPLES, i, frames, bit, (data > 0) );
// printf("Sending a %d\n", phase); if (FSK)
} {
else phase = ((data != 0) * 2) - 1;
{ // printf("Sending a %d\n", phase);
if (data == 0) { }
phase *= -1; else
if ( (ctr - smaller) > 0) {
{ if (data == 0) {
for (int j = 1; j <= smaller; j++) phase *= -1;
buffer[ctr - j] = buffer[ctr - j] * 0.4; if ( (ctr - smaller) > 0)
} {
flip_ctr = ctr; for (int j = 1; j <= smaller; j++)
} buffer[ctr - j] = buffer[ctr - j] * 0.4;
} }
} flip_ctr = ctr;
} }
}
for (i = 1; }
i <= (10 * (HEADER_LEN + DATA_LEN * PAYLOADS + RS_FRAMES * PARITY_LEN) * SAMPLES); i++) // 572 }
{
write_wave(ctr); for (i = 1;
if ( (i % SAMPLES) == 0) { i <= (10 * (HEADER_LEN + DATA_LEN * PAYLOADS + RS_FRAMES * PARITY_LEN) * SAMPLES); i++) // 572
int symbol = (int)((i - 1)/ (SAMPLES * 10)); {
int bit = 10 - (i - symbol * SAMPLES * 10) / SAMPLES + 1; write_wave(ctr);
val = data10[symbol]; if ( (i % SAMPLES) == 0) {
data = val & 1 << (bit - 1); int symbol = (int)((i - 1)/ (SAMPLES * 10));
// printf ("%d i: %d new frame %d data10[%d] = %x bit %d = %d \n", int bit = 10 - (i - symbol * SAMPLES * 10) / SAMPLES + 1;
// ctr/SAMPLES, i, frames, symbol, val, bit, (data > 0) ); val = data10[symbol];
if (FSK) data = val & 1 << (bit - 1);
{ // printf ("%d i: %d new frame %d data10[%d] = %x bit %d = %d \n",
phase = ((data != 0) * 2) - 1; // ctr/SAMPLES, i, frames, symbol, val, bit, (data > 0) );
// printf("Sending a %d\n", phase); if (FSK)
} {
else phase = ((data != 0) * 2) - 1;
{ // printf("Sending a %d\n", phase);
if (data == 0) { }
phase *= -1; else
if ( (ctr - smaller) > 0) {
{ if (data == 0) {
for (int j = 1; j <= smaller; j ++) phase *= -1;
buffer[ctr - j] = buffer[ctr - j] * 0.4; if ( (ctr - smaller) > 0)
} {
flip_ctr = ctr; for (int j = 1; j <= smaller; j ++)
} buffer[ctr - j] = buffer[ctr - j] * 0.4;
} }
} flip_ctr = ctr;
} }
} }
// write_wav("transmit.wav", BUF_LEN, buffer, S_RATE); }
}
}
// write_wav("transmit.wav", BUF_LEN, buffer, S_RATE);
int error = 0; int error = 0;
int count; int count;
@ -766,41 +806,14 @@ int get_tlm_fox() {
// socket write // socket write
struct sockaddr_in address;
int sock = 0, valread;
struct sockaddr_in serv_addr;
// char *hello = "Hello from client";
// char buffer[1024] = {0};
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Socket creation error \n");
error = 1;
}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
{
printf("\nInvalid address/ Address not supported \n");
error = 1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\nConnection Failed \n");
error = 1;
}
if (!error) if (!error)
{ {
printf("Sending buffer over socket!\n"); printf("Sending buffer over socket!\n");
send(sock, buffer, sizeof(buffer), 0); send(sock, buffer, sizeof(buffer), 0);
} }
sleep(20);
return 0; return 0;
} }
@ -844,8 +857,8 @@ return 0;
//static int encodeB(short int *b, int index, int val); //static int encodeB(short int *b, int index, int val);
//static int encodeA(short int *b, int index, int val); //static int encodeA(short int *b, int index, int val);
static int NOT_FRAME = /* 0fa */ 0xfa & 0x3ff; static int NOT_FRAME = /* 0fa */ 0xfa & 0x3ff;
static int FRAME = /* 0fa */ ~0xfa & 0x3ff; static int FRAME = /* 0fa */ ~0xfa & 0x3ff;
/* /*
* TelemEncoding.c * TelemEncoding.c
@ -1003,13 +1016,13 @@ void update_rs(
void write_little_endian(unsigned int word, int num_bytes, FILE *wav_file) void write_little_endian(unsigned int word, int num_bytes, FILE *wav_file)
{ {
unsigned buf; unsigned buf;
while(num_bytes>0) while(num_bytes>0)
{ buf = word & 0xff; { buf = word & 0xff;
fwrite(&buf, 1,1, wav_file); fwrite(&buf, 1,1, wav_file);
num_bytes--; num_bytes--;
word >>= 8; word >>= 8;
} }
} }
/* information about the WAV file format from /* information about the WAV file format from
@ -1020,48 +1033,48 @@ http://ccrma.stanford.edu/courses/422/projects/WaveFormat/
void write_wav(char * filename, unsigned long num_samples, short int * data, int s_rate) void write_wav(char * filename, unsigned long num_samples, short int * data, int s_rate)
{ {
FILE* wav_file; FILE* wav_file;
unsigned int sample_rate; unsigned int sample_rate;
unsigned int num_channels; unsigned int num_channels;
unsigned int bytes_per_sample; unsigned int bytes_per_sample;
unsigned int byte_rate; unsigned int byte_rate;
unsigned long i; /* counter for samples */ unsigned long i; /* counter for samples */
num_channels = 1; /* monoaural */ num_channels = 1; /* monoaural */
bytes_per_sample = 2; bytes_per_sample = 2;
if (s_rate<=0) sample_rate = 44100; if (s_rate<=0) sample_rate = 44100;
else sample_rate = (unsigned int) s_rate; else sample_rate = (unsigned int) s_rate;
byte_rate = sample_rate*num_channels*bytes_per_sample; byte_rate = sample_rate*num_channels*bytes_per_sample;
wav_file = fopen(filename, "w"); wav_file = fopen(filename, "w");
assert(wav_file); /* make sure it opened */ assert(wav_file); /* make sure it opened */
/* write RIFF header */ /* write RIFF header */
fwrite("RIFF", 1, 4, wav_file); fwrite("RIFF", 1, 4, wav_file);
write_little_endian(36 + bytes_per_sample* num_samples*num_channels, 4, wav_file); write_little_endian(36 + bytes_per_sample* num_samples*num_channels, 4, wav_file);
fwrite("WAVE", 1, 4, wav_file); fwrite("WAVE", 1, 4, wav_file);
/* write fmt subchunk */ /* write fmt subchunk */
fwrite("fmt ", 1, 4, wav_file); fwrite("fmt ", 1, 4, wav_file);
write_little_endian(16, 4, wav_file); /* SubChunk1Size is 16 */ write_little_endian(16, 4, wav_file); /* SubChunk1Size is 16 */
write_little_endian(1, 2, wav_file); /* PCM is format 1 */ write_little_endian(1, 2, wav_file); /* PCM is format 1 */
write_little_endian(num_channels, 2, wav_file); write_little_endian(num_channels, 2, wav_file);
write_little_endian(sample_rate, 4, wav_file); write_little_endian(sample_rate, 4, wav_file);
write_little_endian(byte_rate, 4, wav_file); write_little_endian(byte_rate, 4, wav_file);
write_little_endian(num_channels*bytes_per_sample, 2, wav_file); /* block align */ write_little_endian(num_channels*bytes_per_sample, 2, wav_file); /* block align */
write_little_endian(8*bytes_per_sample, 2, wav_file); /* bits/sample */ write_little_endian(8*bytes_per_sample, 2, wav_file); /* bits/sample */
/* write data subchunk */ /* write data subchunk */
fwrite("data", 1, 4, wav_file); fwrite("data", 1, 4, wav_file);
write_little_endian(bytes_per_sample* num_samples*num_channels, 4, wav_file); write_little_endian(bytes_per_sample* num_samples*num_channels, 4, wav_file);
for (i=0; i< num_samples; i++) for (i=0; i< num_samples; i++)
{ write_little_endian((unsigned int)(data[i]),bytes_per_sample, wav_file); { write_little_endian((unsigned int)(data[i]),bytes_per_sample, wav_file);
} }
fclose(wav_file); fclose(wav_file);
} }
@ -1069,28 +1082,28 @@ void write_wav(char * filename, unsigned long num_samples, short int * data, int
//int main(int argc, char * argv[]) //int main(int argc, char * argv[])
//{ //{
// return 0; // return 0;
//} //}
void write_wave(int i) void write_wave(int i)
{ {
if (FSK) if (FSK)
{ {
// if ((ctr - flip_ctr) < smaller) // if ((ctr - flip_ctr) < smaller)
// buffer[ctr++] = 0.1 * phase * (ctr - flip_ctr) / smaller; // buffer[ctr++] = 0.1 * phase * (ctr - flip_ctr) / smaller;
// else // else
buffer[ctr++] = 0.25 * amplitude * phase; buffer[ctr++] = 0.25 * amplitude * phase;
} }
else else
{ {
if ((ctr - flip_ctr) < smaller) if ((ctr - flip_ctr) < smaller)
buffer[ctr++] = (int)(amplitude * 0.4 * phase * buffer[ctr++] = (int)(amplitude * 0.4 * phase *
sin((float)(2*M_PI*i*freq_Hz/S_RATE))); sin((float)(2*M_PI*i*freq_Hz/S_RATE)));
else else
buffer[ctr++] = (int)(amplitude * phase * buffer[ctr++] = (int)(amplitude * phase *
sin((float)(2*M_PI*i*freq_Hz/S_RATE))); sin((float)(2*M_PI*i*freq_Hz/S_RATE)));
} }
// printf("%d %d \n", i, buffer[ctr - 1]); // printf("%d %d \n", i, buffer[ctr - 1]);
} }

Loading…
Cancel
Save

Powered by TurnKey Linux.