doppler shift / Mbed 2 deprecated Register_set_20

Dependencies:   mbed

Committer:
KINU
Date:
Thu Mar 25 14:56:16 2021 +0000
Revision:
6:85891eee5322
Parent:
5:215407735ff3
edcvg

Who changed what in which revision?

UserRevisionLine numberNew contents of line
KINU 0:4f081a382f9b 1 #include <mbed.h>
KINU 0:4f081a382f9b 2 #include <stdio.h>
KINU 0:4f081a382f9b 3 #include <stdlib.h>
KINU 0:4f081a382f9b 4 Serial pc(USBTX, USBRX,921600);
KINU 0:4f081a382f9b 5 Serial device( p9, p10, 9600);
KINU 0:4f081a382f9b 6 #define kHz 1000
KINU 0:4f081a382f9b 7 #define MHz 1000000
KINU 0:4f081a382f9b 8 #define PFD 19.68
KINU 0:4f081a382f9b 9 LocalFileSystem local("local");
KINU 0:4f081a382f9b 10 #pragma diag_suppress 870
KINU 0:4f081a382f9b 11 class calsat32
KINU 0:4f081a382f9b 12 {
KINU 0:4f081a382f9b 13 public:
KINU 0:4f081a382f9b 14 int Fractional_N;
KINU 0:4f081a382f9b 15 int Integer_N;
KINU 0:4f081a382f9b 16 int intbin[8];
KINU 0:4f081a382f9b 17 int decbin[15];
KINU 0:4f081a382f9b 18 int register_data;//レジスタへ入れるファイルから読み込んだ10進数の値
KINU 0:4f081a382f9b 19 int length; //値の長さ
KINU 0:4f081a382f9b 20 int size;//累計の長さこれが32となったら1つのレジスタの設定が完了として次のレジスタの設定へ移る
KINU 0:4f081a382f9b 21 int register_num;//レジスタ番号
KINU 0:4f081a382f9b 22 int buf;
KINU 0:4f081a382f9b 23 char _command[128];
KINU 1:515864d13410 24 double human_freq; //人から受け取る周波数の値
KINU 0:4f081a382f9b 25 uint32_t _register;//最終的に欲しいレジスタの値.32ビット
KINU 2:0a0b5c62d10c 26 uint32_t register_data_array[16];//完成したレジスターを格納する配列
KINU 5:215407735ff3 27 uint32_t reg0;//reg0_gen に渡す用の配列
KINU 0:4f081a382f9b 28 const char* filename;
KINU 0:4f081a382f9b 29 FILE *fp;
KINU 0:4f081a382f9b 30 calsat32()
KINU 0:4f081a382f9b 31 {
KINU 0:4f081a382f9b 32 Fractional_N = 0;
KINU 0:4f081a382f9b 33 Integer_N = 0;
KINU 0:4f081a382f9b 34 register_data = 0;//レジスタへ入れる1ファイルから読み込んだ10進数の値
KINU 0:4f081a382f9b 35 length = 0; //値の長さ
KINU 0:4f081a382f9b 36 size = 0;//累計の長さこれが32となったら1つのレジスタの設定が完了として次のレジスタの設定へ移る
KINU 0:4f081a382f9b 37 register_num = 1;//レジスタ番号
KINU 0:4f081a382f9b 38 filename = "/local/register.tsv";
KINU 0:4f081a382f9b 39 _register = 0;
KINU 1:515864d13410 40 human_freq = 0;
KINU 0:4f081a382f9b 41 }
KINU 0:4f081a382f9b 42 //main関数内でコマンドが0x00の時にmainでbinary()を呼ぶと計算をしてuint_32型の値を返す
KINU 3:e6d72c02e045 43 uint32_t Reg0_gen(int key,double calsat_freq,double human_freq )
KINU 3:e6d72c02e045 44 {
KINU 5:215407735ff3 45 printf("%lf %lf\r\n",calsat_freq,human_freq);
KINU 3:e6d72c02e045 46 uint32_t freq_bit;
KINU 5:215407735ff3 47 uint32_t tmp = 0;
KINU 4:a263257f3a2b 48 if(key == '0') {
KINU 6:85891eee5322 49
KINU 5:215407735ff3 50 printf("\r\n");
KINU 5:215407735ff3 51
KINU 5:215407735ff3 52 tmp = reg0 >> 27; //この行と次の行でレジスタのデータの部分を全て0にしている
KINU 5:215407735ff3 53 tmp = tmp << 27;
KINU 6:85891eee5322 54 /*for(int i = 32; i > 0; i--) {//デバッグ用
KINU 4:a263257f3a2b 55 if(tmp & (1 << (i - 1))) {
KINU 4:a263257f3a2b 56 pc.printf("1");
KINU 4:a263257f3a2b 57 } else {
KINU 4:a263257f3a2b 58 pc.printf("0");
KINU 4:a263257f3a2b 59 }
KINU 4:a263257f3a2b 60 }
KINU 6:85891eee5322 61 printf("\r\n"); //001110000000000…が出力*/
KINU 5:215407735ff3 62
KINU 5:215407735ff3 63 freq_bit = binary(calsat_freq)<<4;
KINU 6:85891eee5322 64 /* printf("freq_bit=");//デバッグ用
KINU 5:215407735ff3 65 for(int i = 32; i > 0; i--) {
KINU 5:215407735ff3 66 if(freq_bit & (1 << (i - 1))) {
KINU 5:215407735ff3 67 pc.printf("1");
KINU 5:215407735ff3 68 } else {
KINU 5:215407735ff3 69 pc.printf("0"); //00000[23bit]0000が出力
KINU 5:215407735ff3 70 }
KINU 5:215407735ff3 71 }
KINU 6:85891eee5322 72 printf("\r\n");*/
KINU 5:215407735ff3 73 /*printf("freq_bit0=");//デバッグ用
KINU 5:215407735ff3 74 for(int i = 32; i > 0; i--) {
KINU 5:215407735ff3 75 if(freq_bit & (1 << (i - 1))) {
KINU 5:215407735ff3 76 pc.printf("1");
KINU 5:215407735ff3 77 } else {
KINU 5:215407735ff3 78 pc.printf("0");
KINU 5:215407735ff3 79 }
KINU 5:215407735ff3 80 }
KINU 5:215407735ff3 81 printf("\r\n");*/
KINU 6:85891eee5322 82
KINU 5:215407735ff3 83
KINU 5:215407735ff3 84 /*freq_bit = freq_bit<<4;
KINU 5:215407735ff3 85 printf("freq_bit4 = ");//デバッグ用
KINU 5:215407735ff3 86 for(int i = 32; i > 0; i--) {
KINU 5:215407735ff3 87 if(freq_bit & (1 << (i - 1))) {
KINU 5:215407735ff3 88 pc.printf("1");
KINU 5:215407735ff3 89 } else {
KINU 5:215407735ff3 90 pc.printf("0");
KINU 5:215407735ff3 91 }
KINU 5:215407735ff3 92 }
KINU 5:215407735ff3 93 printf("\r\n");*/
KINU 5:215407735ff3 94 tmp |= freq_bit;
KINU 5:215407735ff3 95 register_data_array[2] = tmp;
KINU 6:85891eee5322 96
KINU 4:a263257f3a2b 97 } else if(key == '1') {
KINU 5:215407735ff3 98
KINU 4:a263257f3a2b 99 freq_bit = binary(calsat_freq);
KINU 5:215407735ff3 100 tmp = freq_bit<<4;
KINU 3:e6d72c02e045 101
KINU 3:e6d72c02e045 102 } else if(key =='2') { //calsatから受けとる
KINU 3:e6d72c02e045 103 freq_bit = binary(human_freq);
KINU 5:215407735ff3 104 tmp = freq_bit<<4;
KINU 3:e6d72c02e045 105 }
KINU 5:215407735ff3 106 /*else if(key == '3') {
KINU 3:e6d72c02e045 107 uint32_t tmp = 0;
KINU 5:215407735ff3 108 tmp = reg0 >> 27; //この行と次の行でレジスタのデータの部分を全て0にしている
KINU 5:215407735ff3 109 tmp = tmp << 27;
KINU 5:215407735ff3 110 freq_bit = binary(human_freq);
KINU 5:215407735ff3 111 freq_bit = freq_bit<<4;
KINU 5:215407735ff3 112 tmp |= freq_bit;
KINU 3:e6d72c02e045 113 register_data_array[2] = tmp;//配列の中に格納する
KINU 3:e6d72c02e045 114 show_all();
KINU 4:a263257f3a2b 115 */
KINU 3:e6d72c02e045 116 else {}
KINU 3:e6d72c02e045 117
KINU 5:215407735ff3 118 for(int i = 32; i > 0; i--) {
KINU 5:215407735ff3 119 if(tmp & (1 << (i - 1))) {
KINU 2:0a0b5c62d10c 120 pc.printf("1");
KINU 2:0a0b5c62d10c 121 } else {
KINU 2:0a0b5c62d10c 122 pc.printf("0");
KINU 2:0a0b5c62d10c 123 }
KINU 2:0a0b5c62d10c 124 }
KINU 3:e6d72c02e045 125
KINU 3:e6d72c02e045 126 printf("\r\n");
KINU 3:e6d72c02e045 127 return freq_bit;
KINU 3:e6d72c02e045 128
KINU 1:515864d13410 129 }
KINU 0:4f081a382f9b 130 void Shift_bit(uint32_t& Register, int Register_data, int Size)
KINU 0:4f081a382f9b 131 {
KINU 0:4f081a382f9b 132 Register |= (Register_data << Size);
KINU 0:4f081a382f9b 133 }
KINU 3:e6d72c02e045 134 void file_read(char key,double calsat_freq)
KINU 0:4f081a382f9b 135 {
KINU 0:4f081a382f9b 136 /* ファイルのオープン */
KINU 0:4f081a382f9b 137 char name[100] = {};//出力はしない
KINU 0:4f081a382f9b 138 if ((fp = fopen("/local/register.tsv", "r")) == NULL) {
KINU 0:4f081a382f9b 139 fprintf(stderr, "%sのオープンに失敗しました.\r\n", filename);
KINU 0:4f081a382f9b 140 exit(EXIT_FAILURE);
KINU 0:4f081a382f9b 141 }
KINU 0:4f081a382f9b 142 /* ファイルの終端まで文字を読み取り表示する */
KINU 0:4f081a382f9b 143 int num[5] = {1,3,0,5,4};
KINU 0:4f081a382f9b 144 int l = 1;//行数
KINU 0:4f081a382f9b 145 uint32_t _register = 0;//最終的に欲しいレジスタの値.32ビット
KINU 0:4f081a382f9b 146 while ( fscanf(fp,"%s%d%d%d",name, &register_data,&length,&buf) != EOF ) {
KINU 0:4f081a382f9b 147 if(l == 1 || l == 14 || l == 21 ||l == 27 || l == 37) {
KINU 0:4f081a382f9b 148 printf("%d\r\n",l);
KINU 0:4f081a382f9b 149 l++;
KINU 0:4f081a382f9b 150 } else {
KINU 0:4f081a382f9b 151 printf(" %d %d %d \r\n",register_data, length,size);
KINU 0:4f081a382f9b 152 if(l==23) {
KINU 3:e6d72c02e045 153 /*if(key == '1')
KINU 1:515864d13410 154 Shift_bit(_register, binary(calsat_freq), 4);
KINU 1:515864d13410 155 if(key == '2')*/
KINU 3:e6d72c02e045 156 human_freq = register_data;
KINU 3:e6d72c02e045 157 // Shift_bit(_register, binary(register_data), 4);
KINU 3:e6d72c02e045 158 _register = Reg0_gen(key, calsat_freq, human_freq );
KINU 3:e6d72c02e045 159
KINU 3:e6d72c02e045 160 for(int i = 27; i > 0; i--) {//デバッグ用
KINU 3:e6d72c02e045 161 if(_register & (1 << (i - 1))) {
KINU 3:e6d72c02e045 162 pc.printf("1");
KINU 3:e6d72c02e045 163 } else {
KINU 3:e6d72c02e045 164 pc.printf("0");
KINU 3:e6d72c02e045 165 }
KINU 3:e6d72c02e045 166 }
KINU 4:a263257f3a2b 167 printf("\r\n");
KINU 4:a263257f3a2b 168 size = 27;
KINU 0:4f081a382f9b 169 //テキストファイルから受け取った値を格納するとき
KINU 0:4f081a382f9b 170 } else {
KINU 0:4f081a382f9b 171 Shift_bit(_register, register_data, size);
KINU 0:4f081a382f9b 172 size += length;
KINU 0:4f081a382f9b 173 if(register_num == 1 && size == 26) {
KINU 0:4f081a382f9b 174 printf(" = reg_%d ",num[register_num - 1]);
KINU 0:4f081a382f9b 175 show(_register);
KINU 0:4f081a382f9b 176 register_num++;
KINU 2:0a0b5c62d10c 177 //_register = 0;
KINU 0:4f081a382f9b 178 size = 0;
KINU 0:4f081a382f9b 179 }
KINU 0:4f081a382f9b 180 if(size == 32) {
KINU 2:0a0b5c62d10c 181 //これで一つ完成.
KINU 0:4f081a382f9b 182 printf(" = reg_%d ",num[register_num - 1]);
KINU 0:4f081a382f9b 183 show(_register);
KINU 0:4f081a382f9b 184 register_num++;
KINU 0:4f081a382f9b 185 _register = 0;
KINU 0:4f081a382f9b 186 size = 0;
KINU 0:4f081a382f9b 187 }
KINU 0:4f081a382f9b 188 }
KINU 0:4f081a382f9b 189 l++;
KINU 0:4f081a382f9b 190 }
KINU 0:4f081a382f9b 191 }
KINU 0:4f081a382f9b 192 /* ファイルのクローズ */
KINU 0:4f081a382f9b 193 fclose(fp);
KINU 0:4f081a382f9b 194 }
KINU 4:a263257f3a2b 195 uint32_t binary(double RFout)
KINU 0:4f081a382f9b 196 {
KINU 0:4f081a382f9b 197 double n = RFout * 2 / PFD;
KINU 0:4f081a382f9b 198 Integer_N = (int)n;
KINU 0:4f081a382f9b 199 double test = (n - (int)n);
KINU 0:4f081a382f9b 200 Fractional_N = test*32768;
KINU 0:4f081a382f9b 201 uint32_t data = Fractional_N;
KINU 0:4f081a382f9b 202 return data |= (Integer_N << 15);//レジスタ入力に適した形にした23桁の2進数を返す
KINU 0:4f081a382f9b 203 }
KINU 0:4f081a382f9b 204 //データを取得し返す関数
KINU 0:4f081a382f9b 205 double doppler(char *a)
KINU 0:4f081a382f9b 206 {
KINU 0:4f081a382f9b 207 int data[10] = {0};
KINU 1:515864d13410 208 double calsat_freq = 0;
KINU 0:4f081a382f9b 209 int flag = 0;
KINU 0:4f081a382f9b 210 for(int i = 5; i < 10; i++) {
KINU 0:4f081a382f9b 211 char c = a[i];
KINU 0:4f081a382f9b 212 data[flag] = (c >> 4) & 0xf;
KINU 0:4f081a382f9b 213 data[flag+1] = c & 0xf;
KINU 0:4f081a382f9b 214 flag += 2;
KINU 0:4f081a382f9b 215 }
KINU 1:515864d13410 216 calsat_freq = 10 * data[0] + data[1] + kHz * data[2] + 100 * data[3] + 100*kHz * data[4] + 10*kHz * data[5] + 10*MHz * data[6] + MHz * data[7] + data[8] + 100*MHz * data[9];//Hz
KINU 3:e6d72c02e045 217 printf("\n%lf\r\n",calsat_freq / 1000000); //Mhz
KINU 1:515864d13410 218 return calsat_freq / 1000000;
KINU 0:4f081a382f9b 219 }
KINU 0:4f081a382f9b 220 void show(uint32_t bin)
KINU 0:4f081a382f9b 221 {
KINU 0:4f081a382f9b 222 static int reg_num = 0;
KINU 0:4f081a382f9b 223 //表示フェーズ
KINU 0:4f081a382f9b 224 for(int i = 32; i > 0; i--) {
KINU 0:4f081a382f9b 225 if(bin & (1 << (i - 1))) {
KINU 0:4f081a382f9b 226 pc.printf("1");
KINU 0:4f081a382f9b 227 } else {
KINU 0:4f081a382f9b 228 pc.printf("0");
KINU 0:4f081a382f9b 229 }
KINU 0:4f081a382f9b 230 }
KINU 0:4f081a382f9b 231 printf("\r\n");
KINU 0:4f081a382f9b 232 //格納フェーズ
KINU 0:4f081a382f9b 233 register_data_array[reg_num] = bin;
KINU 5:215407735ff3 234 if(reg_num == 2)
KINU 5:215407735ff3 235 reg0 = bin;
KINU 0:4f081a382f9b 236 reg_num++;
KINU 0:4f081a382f9b 237 if(reg_num == 4) {
KINU 0:4f081a382f9b 238 reg_num = 0;
KINU 6:85891eee5322 239 register_num =0;
KINU 0:4f081a382f9b 240 }
KINU 0:4f081a382f9b 241 }
KINU 0:4f081a382f9b 242 void show_all()//完成したレジスタの値を出力
KINU 0:4f081a382f9b 243 {
KINU 0:4f081a382f9b 244 for(int j = 0; j < 5; j++) {
KINU 0:4f081a382f9b 245 for(int i = 32; i > 0; i--) {
KINU 0:4f081a382f9b 246 if(register_data_array[j] & (1 << (i - 1))) {
KINU 0:4f081a382f9b 247 pc.printf("1");
KINU 0:4f081a382f9b 248 } else {
KINU 0:4f081a382f9b 249 pc.printf("0");
KINU 0:4f081a382f9b 250 }
KINU 0:4f081a382f9b 251 }
KINU 0:4f081a382f9b 252 printf("\r\n");
KINU 0:4f081a382f9b 253 }
KINU 0:4f081a382f9b 254 }
KINU 1:515864d13410 255 double packet(char c)
KINU 0:4f081a382f9b 256 {
KINU 0:4f081a382f9b 257 static int a = 0;
KINU 1:515864d13410 258 double calsat_freq;
KINU 0:4f081a382f9b 259 _command[a] = c;
KINU 0:4f081a382f9b 260 a++;
KINU 0:4f081a382f9b 261 if(_command[a-1] == 0xfd) {
KINU 3:e6d72c02e045 262 calsat_freq = doppler(_command);
KINU 5:215407735ff3 263 _register = Reg0_gen('0', calsat_freq, human_freq); //reg0をcalsatの値で更新
KINU 0:4f081a382f9b 264 /*
KINU 0:4f081a382f9b 265 for(int j = 0; j < a; j++)
KINU 0:4f081a382f9b 266 printf("%02hhx",_command[j]);
KINU 0:4f081a382f9b 267 printf("\r\n");
KINU 0:4f081a382f9b 268 */
KINU 0:4f081a382f9b 269 a = 0;
KINU 0:4f081a382f9b 270 }
KINU 1:515864d13410 271 return calsat_freq;
KINU 0:4f081a382f9b 272 }
KINU 1:515864d13410 273 void check(char key,double calsat_freq)
KINU 0:4f081a382f9b 274 {
KINU 2:0a0b5c62d10c 275 printf("key = %c\r\n",key);
KINU 0:4f081a382f9b 276 if(_command[4] == 0x00) {
KINU 0:4f081a382f9b 277 printf("---------------------------Start-----------------------------\r\n");
KINU 1:515864d13410 278 file_read(key,calsat_freq);
KINU 0:4f081a382f9b 279 }
KINU 0:4f081a382f9b 280 }
KINU 0:4f081a382f9b 281 };
KINU 0:4f081a382f9b 282 int main()
KINU 0:4f081a382f9b 283 {
KINU 0:4f081a382f9b 284 Serial device( p9, p10, 9600);
KINU 2:0a0b5c62d10c 285 printf("Start\r\n");
KINU 6:85891eee5322 286 calsat32 tmp;
KINU 6:85891eee5322 287 tmp.file_read('0',435);
KINU 0:4f081a382f9b 288 while(1) {
KINU 6:85891eee5322 289
KINU 0:4f081a382f9b 290 while(device.readable()) {
KINU 3:e6d72c02e045 291
KINU 0:4f081a382f9b 292 char c = device.getc();
KINU 1:515864d13410 293 double calsat_freq = tmp.packet(c);
KINU 0:4f081a382f9b 294 if(pc.readable()) {
KINU 0:4f081a382f9b 295 char key = pc.getc();
KINU 3:e6d72c02e045 296 if(key == '1' ||key == '2') {
KINU 3:e6d72c02e045 297 tmp.check(key,calsat_freq);
KINU 3:e6d72c02e045 298 //file読み込み、calsator人の更新
KINU 3:e6d72c02e045 299 } else if(key == '3') //reg0を人にする
KINU 3:e6d72c02e045 300 tmp.check(key,calsat_freq);
KINU 5:215407735ff3 301 else if(key == '0') //reg0を人にする
KINU 4:a263257f3a2b 302 tmp.check(key,calsat_freq);
KINU 0:4f081a382f9b 303 }
KINU 0:4f081a382f9b 304 }
KINU 0:4f081a382f9b 305 }
KINU 0:4f081a382f9b 306 return 0;
KINU 0:4f081a382f9b 307 }