introduce a lot of errors when trying to apply model
This commit is contained in:
@@ -21,16 +21,17 @@
|
||||
#include <fcntl.h>
|
||||
#include <math.h>
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "dbg.h"
|
||||
#include "main.h"
|
||||
#include "movingmodel.h"
|
||||
#include "serial.h"
|
||||
|
||||
// serial devices FD
|
||||
@@ -55,45 +56,6 @@ typedef struct __attribute__((packed)){
|
||||
uint8_t CRC[4];
|
||||
} enc_t;
|
||||
|
||||
/**
|
||||
* @brief dtime - monotonic time from first run
|
||||
* @return
|
||||
*/
|
||||
double dtime(){
|
||||
struct timespec start_time = {0}, cur_time;
|
||||
if(start_time.tv_sec == 0 && start_time.tv_nsec == 0){
|
||||
clock_gettime(CLOCK_MONOTONIC, &start_time);
|
||||
}
|
||||
clock_gettime(CLOCK_MONOTONIC, &cur_time);
|
||||
return ((double)(cur_time.tv_sec - start_time.tv_sec) +
|
||||
(cur_time.tv_nsec - start_time.tv_nsec) * 1e-9);
|
||||
}
|
||||
#if 0
|
||||
double dtime(){
|
||||
double t;
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
t = tv.tv_sec + ((double)tv.tv_usec)/1e6;
|
||||
return t;
|
||||
}
|
||||
#endif
|
||||
#if 0
|
||||
double tv2d(struct timeval *tv){
|
||||
if(!tv) return 0.;
|
||||
double t = tv->tv_sec + ((double)tv->tv_usec) / 1e6;
|
||||
return t;
|
||||
}
|
||||
#endif
|
||||
#if 0
|
||||
// init start time
|
||||
static void gttime(){
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
tv_sec_got = tv.tv_sec;
|
||||
tv_usec_got = tv.tv_usec;
|
||||
}
|
||||
#endif
|
||||
|
||||
// calculate current X/Y speeds
|
||||
static void getXspeed(double t){
|
||||
mountdata.encXspeed.val = (mountdata.encXposition.val - lastXenc.val) / (t - lastXenc.t);
|
||||
@@ -170,7 +132,7 @@ static int getencval(int fd, double *val, double *t){
|
||||
if(fd < 0) return FALSE;
|
||||
char buf[128];
|
||||
int got = 0, Lmax = 127;
|
||||
double t0 = dtime();
|
||||
double t0 = nanotime();
|
||||
do{
|
||||
fd_set rfds;
|
||||
FD_ZERO(&rfds);
|
||||
@@ -189,7 +151,7 @@ static int getencval(int fd, double *val, double *t){
|
||||
buf[got] = 0;
|
||||
} else continue;
|
||||
if(strchr(buf, '\n')) break;
|
||||
}while(Lmax && dtime() - t0 < Conf.EncoderReqInterval);
|
||||
}while(Lmax && nanotime() - t0 < Conf.EncoderReqInterval);
|
||||
if(got == 0) return 0; // WTF?
|
||||
char *estr = strrchr(buf, '\n');
|
||||
if(!estr) return 0;
|
||||
@@ -277,7 +239,7 @@ static void *encoderthread1(void _U_ *u){
|
||||
if((uint8_t)b == ENC_MAGICK){
|
||||
// DBG("Got magic -> start filling packet");
|
||||
databuf[wridx++] = (uint8_t) b;
|
||||
t = dtime();
|
||||
t = nanotime();
|
||||
}
|
||||
continue;
|
||||
}else databuf[wridx++] = (uint8_t) b;
|
||||
@@ -298,13 +260,13 @@ static void *encoderthread2(void _U_ *u){
|
||||
if(Conf.SepEncoder != 2) return NULL;
|
||||
DBG("Thread started");
|
||||
int errctr = 0;
|
||||
double t0 = dtime();
|
||||
const char *req = "next\n";
|
||||
double t0 = nanotime();
|
||||
const char *req = "\n";
|
||||
int need2ask = 0; // need or not to ask encoder for new data
|
||||
while(encfd[0] > -1 && encfd[1] > -1 && errctr < MAX_ERR_CTR){
|
||||
if(need2ask){
|
||||
if(5 != write(encfd[0], req, 5)) { ++errctr; continue; }
|
||||
else if(5 != write(encfd[1], req, 5)) { ++errctr; continue; }
|
||||
if(1 != write(encfd[0], req, 1)) { ++errctr; continue; }
|
||||
else if(1 != write(encfd[1], req, 1)) { ++errctr; continue; }
|
||||
}
|
||||
double v, t;
|
||||
if(getencval(encfd[0], &v, &t)){
|
||||
@@ -329,9 +291,9 @@ static void *encoderthread2(void _U_ *u){
|
||||
else need2ask = 1;
|
||||
continue;
|
||||
}
|
||||
while(dtime() - t0 < Conf.EncoderReqInterval){ usleep(10); }
|
||||
//DBG("DT=%g (RI=%g)", dtime()-t0, Conf.EncoderReqInterval);
|
||||
t0 = dtime();
|
||||
while(nanotime() - t0 < Conf.EncoderReqInterval){ usleep(50); }
|
||||
//DBG("DT=%g (RI=%g)", nanotime()-t0, Conf.EncoderReqInterval);
|
||||
t0 = nanotime();
|
||||
}
|
||||
DBG("ERRCTR=%d", errctr);
|
||||
for(int i = 0; i < 2; ++i){
|
||||
@@ -364,20 +326,25 @@ static void *mountthread(void _U_ *u){
|
||||
int errctr = 0;
|
||||
uint8_t buf[2*sizeof(SSstat)];
|
||||
SSstat *status = (SSstat*) buf;
|
||||
if(Conf.RunModel)
|
||||
while(1){
|
||||
pthread_mutex_lock(&datamutex);
|
||||
// now change data
|
||||
getModData(&mountdata);
|
||||
pthread_mutex_unlock(&datamutex);
|
||||
double t0 = nanotime();
|
||||
while(nanotime() - t0 < Conf.MountReqInterval) usleep(500);
|
||||
t0 = nanotime();
|
||||
}
|
||||
// data to get
|
||||
data_t d = {.buf = buf, .maxlen = sizeof(buf)};
|
||||
// cmd to send
|
||||
data_t *cmd_getstat = cmd2dat(CMD_GETSTAT);
|
||||
if(!cmd_getstat) goto failed;
|
||||
double t0 = dtime();
|
||||
/*
|
||||
#ifdef EBUG
|
||||
double t00 = t0;
|
||||
#endif
|
||||
*/
|
||||
while(mntfd > -1 && errctr < MAX_ERR_CTR){
|
||||
// read data to status
|
||||
double tgot = dtime();
|
||||
double tgot = nanotime(), t0 = tgot;
|
||||
DBG("tgot=%g", tgot);
|
||||
if(!MountWriteRead(cmd_getstat, &d) || d.len != sizeof(SSstat)){
|
||||
#ifdef EBUG
|
||||
DBG("Can't read SSstat, need %zd got %zd bytes", sizeof(SSstat), d.len);
|
||||
@@ -396,10 +363,8 @@ static void *mountthread(void _U_ *u){
|
||||
SSconvstat(status, &mountdata, tgot);
|
||||
pthread_mutex_unlock(&datamutex);
|
||||
// allow writing & getters
|
||||
//DBG("t0=%g, tnow=%g", t0-t00, dtime()-t00);
|
||||
if(dtime() - t0 >= Conf.MountReqInterval) usleep(50);
|
||||
while(dtime() - t0 < Conf.MountReqInterval);
|
||||
t0 = dtime();
|
||||
while(nanotime() - t0 < Conf.MountReqInterval) usleep(500);
|
||||
t0 = nanotime();
|
||||
}
|
||||
data_free(&cmd_getstat);
|
||||
failed:
|
||||
@@ -435,6 +400,7 @@ static int ttyopen(const char *path, speed_t speed){
|
||||
|
||||
// return FALSE if failed
|
||||
int openEncoder(){
|
||||
if(Conf.RunModel) return TRUE;
|
||||
if(!Conf.SepEncoder) return FALSE; // try to open separate encoder when it's absent
|
||||
if(Conf.SepEncoder == 1){ // only one device
|
||||
DBG("One device");
|
||||
@@ -442,7 +408,7 @@ int openEncoder(){
|
||||
encfd[0] = ttyopen(Conf.EncoderDevPath, (speed_t) Conf.EncoderDevSpeed);
|
||||
if(encfd[0] < 0) return FALSE;
|
||||
encRtmout.tv_sec = 0;
|
||||
encRtmout.tv_usec = 200000000 / Conf.EncoderDevSpeed; // 20 bytes
|
||||
encRtmout.tv_usec = 100000000 / Conf.EncoderDevSpeed; // 10 bytes
|
||||
if(pthread_create(&encthread, NULL, encoderthread1, NULL)){
|
||||
close(encfd[0]);
|
||||
encfd[0] = -1;
|
||||
@@ -472,6 +438,7 @@ int openEncoder(){
|
||||
|
||||
// return FALSE if failed
|
||||
int openMount(){
|
||||
if(Conf.RunModel) return TRUE;
|
||||
if(mntfd > -1) close(mntfd);
|
||||
DBG("Open mount %s @ %d", Conf.MountDevPath, Conf.MountDevSpeed);
|
||||
mntfd = ttyopen(Conf.MountDevPath, (speed_t) Conf.MountDevSpeed);
|
||||
@@ -487,7 +454,7 @@ int openMount(){
|
||||
DBG("got %zd", l);
|
||||
}while(1);*/
|
||||
mntRtmout.tv_sec = 0;
|
||||
mntRtmout.tv_usec = 500000000 / Conf.MountDevSpeed; // 50 bytes
|
||||
mntRtmout.tv_usec = 500000000 / Conf.MountDevSpeed; // 50 bytes * 10bits / speed
|
||||
if(pthread_create(&mntthread, NULL, mountthread, NULL)){
|
||||
DBG("Can't create thread");
|
||||
close(mntfd);
|
||||
@@ -500,21 +467,22 @@ int openMount(){
|
||||
|
||||
// close all opened serial devices and quit threads
|
||||
void closeSerial(){
|
||||
if(Conf.RunModel) return;
|
||||
if(mntfd > -1){
|
||||
DBG("Kill mount thread");
|
||||
DBG("Cancel mount thread");
|
||||
pthread_cancel(mntthread);
|
||||
DBG("join mount thread");
|
||||
pthread_join(mntthread, NULL);
|
||||
DBG("close fd");
|
||||
DBG("close mount fd");
|
||||
close(mntfd);
|
||||
mntfd = -1;
|
||||
}
|
||||
if(encfd[0] > -1){
|
||||
DBG("Kill encoder thread");
|
||||
DBG("Cancel encoder thread");
|
||||
pthread_cancel(encthread);
|
||||
DBG("join encoder thread");
|
||||
pthread_join(encthread, NULL);
|
||||
DBG("close fd");
|
||||
DBG("close encoder's fd");
|
||||
close(encfd[0]);
|
||||
encfd[0] = -1;
|
||||
if(Conf.SepEncoder == 2){
|
||||
@@ -548,7 +516,7 @@ static int wr(const data_t *out, data_t *in, int needeol){
|
||||
DBG("written bytes not equal to need");
|
||||
return FALSE;
|
||||
}
|
||||
//DBG("Send to mount %zd bytes: %s", out->len, out->buf);
|
||||
DBG("Send to mount %zd bytes: %s", out->len, out->buf);
|
||||
if(needeol){
|
||||
int g = write(mntfd, "\r", 1); // add EOL
|
||||
(void) g;
|
||||
@@ -563,8 +531,9 @@ static int wr(const data_t *out, data_t *in, int needeol){
|
||||
if(b < 0) break; // nothing to read -> go out
|
||||
in->buf[in->len++] = (uint8_t) b;
|
||||
}
|
||||
DBG("got %zd bytes", in->len);
|
||||
//DBG("Clear trashing input");
|
||||
while(getmntbyte() > -1);
|
||||
//while(getmntbyte() > -1);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
@@ -575,6 +544,7 @@ static int wr(const data_t *out, data_t *in, int needeol){
|
||||
* @return FALSE if failed
|
||||
*/
|
||||
int MountWriteRead(const data_t *out, data_t *in){
|
||||
if(Conf.RunModel) return -1;
|
||||
pthread_mutex_lock(&mntmutex);
|
||||
int ret = wr(out, in, 1);
|
||||
pthread_mutex_unlock(&mntmutex);
|
||||
@@ -582,6 +552,7 @@ int MountWriteRead(const data_t *out, data_t *in){
|
||||
}
|
||||
// send binary data - without EOL
|
||||
int MountWriteReadRaw(const data_t *out, data_t *in){
|
||||
if(Conf.RunModel) return -1;
|
||||
pthread_mutex_lock(&mntmutex);
|
||||
int ret = wr(out, in, 0);
|
||||
pthread_mutex_unlock(&mntmutex);
|
||||
@@ -605,6 +576,7 @@ static void loglcmd(SSlcmd *c){
|
||||
|
||||
// send short/long binary command; return FALSE if failed
|
||||
static int bincmd(uint8_t *cmd, int len){
|
||||
if(Conf.RunModel) return FALSE;
|
||||
static data_t *dscmd = NULL, *dlcmd = NULL;
|
||||
if(!dscmd) dscmd = cmd2dat(CMD_SHORTCMD);
|
||||
if(!dlcmd) dlcmd = cmd2dat(CMD_LONGCMD);
|
||||
@@ -641,6 +613,7 @@ rtn:
|
||||
return ret;
|
||||
}
|
||||
|
||||
// short, long and config text-binary commands
|
||||
// return TRUE if OK
|
||||
int cmdS(SSscmd *cmd){
|
||||
return bincmd((uint8_t *)cmd, sizeof(SSscmd));
|
||||
@@ -650,6 +623,7 @@ int cmdL(SSlcmd *cmd){
|
||||
}
|
||||
// rw == 1 to write, 0 to read
|
||||
int cmdC(SSconfig *conf, int rw){
|
||||
if(Conf.RunModel) return FALSE;
|
||||
static data_t *wcmd = NULL, *rcmd = NULL;
|
||||
int ret = FALSE;
|
||||
// dummy buffer to clear trash in input
|
||||
|
||||
Reference in New Issue
Block a user