Feng Hong / Mbed OS Nucleo_rtos_basic
Committer:
hi1000
Date:
Sat Jul 20 03:57:26 2019 +0000
Revision:
18:e6ed582f7022
Parent:
17:faa4d4976d22
Child:
19:0356e54240cc
Add Menu: Calibration Weight and Calibration

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hi1000 7:e0c7e624c5fa 1 #include "mbed.h"
hi1000 18:e6ed582f7022 2 #include "yoda2.h"
hi1000 7:e0c7e624c5fa 3
hi1000 8:6105ffbaf237 4 extern unsigned char rx_buffer[8], tx_buffer[8];
hi1000 7:e0c7e624c5fa 5 extern unsigned char rx_length, tx_length;
hi1000 9:486f65124378 6 extern uint8_t can_tx_data[8];
hi1000 9:486f65124378 7 extern uint8_t can_rx_data[8];
hi1000 17:faa4d4976d22 8 extern int current_weight;
hi1000 16:f4277e9b8612 9
hi1000 16:f4277e9b8612 10 typedef struct can_id_d {
hi1000 16:f4277e9b8612 11 int dest_id;
hi1000 16:f4277e9b8612 12 int source_id;
hi1000 16:f4277e9b8612 13 int message_id;
hi1000 16:f4277e9b8612 14 };
hi1000 16:f4277e9b8612 15
hi1000 16:f4277e9b8612 16 typedef struct data_field_d {
hi1000 16:f4277e9b8612 17 uint8_t cmd;
hi1000 16:f4277e9b8612 18 uint16_t value1;
hi1000 16:f4277e9b8612 19 uint16_t value2;
hi1000 16:f4277e9b8612 20 uint16_t value3;
hi1000 16:f4277e9b8612 21 uint8_t value4;
hi1000 16:f4277e9b8612 22 };
hi1000 16:f4277e9b8612 23 can_id_d can_id_s;
hi1000 16:f4277e9b8612 24 data_field_d can_rxdata_frame;
hi1000 17:faa4d4976d22 25 data_field_d can_txdata_frame;
hi1000 9:486f65124378 26 CANMessage tx_message;
hi1000 9:486f65124378 27 extern MemoryPool<CANMessage, 16> can_mpool;
hi1000 9:486f65124378 28 extern Queue<CANMessage, 16> can_queue;
hi1000 17:faa4d4976d22 29 extern void can_sendData(int can_id, uint8_t *tx_data, int length);
hi1000 18:e6ed582f7022 30 extern Device_Type_d device_type_v;
hi1000 18:e6ed582f7022 31 extern int device_address; // last 8 bits of address
hi1000 18:e6ed582f7022 32 extern int device_type; // first 3 bits of adddress
hi1000 18:e6ed582f7022 33
hi1000 18:e6ed582f7022 34 void handleCupTrackCommand(data_field_d data_pack)
hi1000 18:e6ed582f7022 35 {
hi1000 18:e6ed582f7022 36 }
hi1000 18:e6ed582f7022 37 void handleJamTrackCommand(data_field_d data_pack)
hi1000 18:e6ed582f7022 38 {
hi1000 18:e6ed582f7022 39 }
hi1000 18:e6ed582f7022 40 void handleTeaTrackCommand(data_field_d data_pack)
hi1000 18:e6ed582f7022 41 {
hi1000 18:e6ed582f7022 42 }
hi1000 18:e6ed582f7022 43 void handleJamCommand(data_field_d data_pack)
hi1000 18:e6ed582f7022 44 {
hi1000 18:e6ed582f7022 45 }
hi1000 18:e6ed582f7022 46 void handleTeaCommand(data_field_d data_pack)
hi1000 18:e6ed582f7022 47 {
hi1000 18:e6ed582f7022 48 int can_id;
hi1000 18:e6ed582f7022 49 switch (data_pack.cmd)
hi1000 18:e6ed582f7022 50 {
hi1000 18:e6ed582f7022 51 case COMMAND_INIT:
hi1000 18:e6ed582f7022 52 can_txdata_frame.cmd = COMMAND_INIT;
hi1000 18:e6ed582f7022 53 can_txdata_frame.value1 = current_weight;
hi1000 18:e6ed582f7022 54 can_txdata_frame.value2 = 0;
hi1000 18:e6ed582f7022 55 can_txdata_frame.value3 = 0;
hi1000 18:e6ed582f7022 56 can_txdata_frame.value4 = 0;
hi1000 18:e6ed582f7022 57 can_tx_data[0] = can_txdata_frame.cmd;
hi1000 18:e6ed582f7022 58 can_tx_data[1] = (can_txdata_frame.value1 & 0x00FF);
hi1000 18:e6ed582f7022 59 can_tx_data[2] = ((can_txdata_frame.value1 >> 8) & 0x00FF);
hi1000 18:e6ed582f7022 60 can_tx_data[3] = (can_txdata_frame.value2 & 0x00FF);
hi1000 18:e6ed582f7022 61 can_tx_data[4] = ((can_txdata_frame.value2 >> 8) & 0x00FF);
hi1000 18:e6ed582f7022 62 can_tx_data[5] = (can_txdata_frame.value3 & 0x00FF);
hi1000 18:e6ed582f7022 63 can_tx_data[6] = ((can_txdata_frame.value3 >> 8) & 0x00FF);
hi1000 18:e6ed582f7022 64 can_tx_data[7] = can_txdata_frame.value4;
hi1000 18:e6ed582f7022 65 can_id = (can_id_s.source_id << 18) | (can_id_s.dest_id << 7) | can_id_s.message_id | 0x80000000;
hi1000 18:e6ed582f7022 66 can_sendData(can_id, can_tx_data, 8);
hi1000 18:e6ed582f7022 67 break;
hi1000 18:e6ed582f7022 68 default:
hi1000 18:e6ed582f7022 69 break;
hi1000 18:e6ed582f7022 70 }
hi1000 18:e6ed582f7022 71 }
hi1000 18:e6ed582f7022 72 void handleShakerCommand(data_field_d command)
hi1000 18:e6ed582f7022 73 {
hi1000 18:e6ed582f7022 74 }
hi1000 17:faa4d4976d22 75
hi1000 7:e0c7e624c5fa 76 void analyzePayload()
hi1000 7:e0c7e624c5fa 77 {
hi1000 17:faa4d4976d22 78 int can_id;
hi1000 9:486f65124378 79 int loop;
hi1000 8:6105ffbaf237 80
hi1000 8:6105ffbaf237 81 printf("analyzePayload thread\r\n");
hi1000 8:6105ffbaf237 82 while (true) {
hi1000 8:6105ffbaf237 83 osEvent evt = can_queue.get();
hi1000 8:6105ffbaf237 84 if (evt.status == osEventMessage) {
hi1000 8:6105ffbaf237 85 CANMessage *message = (CANMessage*)evt.value.p;
hi1000 9:486f65124378 86 memcpy((void *)&tx_message, (void *)message, sizeof(tx_message));
hi1000 8:6105ffbaf237 87
hi1000 9:486f65124378 88 printf("analyzePayload got message id=%d 0x%08x\r\n", tx_message.id, tx_message.id);
hi1000 16:f4277e9b8612 89
hi1000 9:486f65124378 90 for (loop = 0; loop < tx_message.len; loop++)
hi1000 9:486f65124378 91 {
hi1000 9:486f65124378 92 can_rx_data[loop] = tx_message.data[loop];
hi1000 9:486f65124378 93 }
hi1000 9:486f65124378 94
hi1000 9:486f65124378 95 printf("analyzePayload got data: length:%d\r\n", tx_message.len);
hi1000 9:486f65124378 96 for (loop = 0; loop < tx_message.len; loop++)
hi1000 9:486f65124378 97 {
hi1000 9:486f65124378 98 printf("data[%d]=%d\r\n", loop, can_rx_data[loop]);
hi1000 9:486f65124378 99 }
hi1000 16:f4277e9b8612 100 can_id_s.dest_id = (tx_message.id & 0x1FFC0000) >> 18;
hi1000 16:f4277e9b8612 101 can_id_s.source_id = (tx_message.id & 0x0003FF80) >> 7;
hi1000 16:f4277e9b8612 102 can_id_s.message_id = (tx_message.id & 0x0000007F) >> 0;
hi1000 16:f4277e9b8612 103 // memcpy((void*)&can_rxdata_frame, (void*)can_rx_data, sizeof(can_rx_data));
hi1000 16:f4277e9b8612 104 can_mpool.free(message);
hi1000 16:f4277e9b8612 105 printf("dest_id=%d source_id=%d message_id=%d \r\n", can_id_s.dest_id, can_id_s.source_id, can_id_s.message_id);
hi1000 16:f4277e9b8612 106 can_rxdata_frame.cmd = can_rx_data[0];
hi1000 16:f4277e9b8612 107 can_rxdata_frame.value1 = (can_rx_data[2] << 8 ) | can_rx_data[1];
hi1000 16:f4277e9b8612 108 can_rxdata_frame.value2 = (can_rx_data[4] << 8 ) | can_rx_data[3];
hi1000 16:f4277e9b8612 109 can_rxdata_frame.value3 = (can_rx_data[6] << 8 ) | can_rx_data[5];
hi1000 16:f4277e9b8612 110 can_rxdata_frame.value4 = can_rx_data[7];
hi1000 16:f4277e9b8612 111 printf("cmd=%d, value1=0x%04x, value2=0x%04x, value3=0x%04x, value4=0x%02x \r\n", can_rxdata_frame.cmd, can_rxdata_frame.value1, can_rxdata_frame.value2, can_rxdata_frame.value3, can_rxdata_frame.value4);
hi1000 18:e6ed582f7022 112 device_type_v = (Device_Type_d)device_type;
hi1000 18:e6ed582f7022 113 switch (device_type_v)
hi1000 17:faa4d4976d22 114 {
hi1000 18:e6ed582f7022 115 case CupTrack:
hi1000 18:e6ed582f7022 116 printf("CupTrack command: %d \r\n", can_rxdata_frame.cmd);
hi1000 18:e6ed582f7022 117 handleCupTrackCommand(can_rxdata_frame);
hi1000 18:e6ed582f7022 118 break;
hi1000 18:e6ed582f7022 119 case JamTrack:
hi1000 18:e6ed582f7022 120 printf("JamTrack command: %d \r\n", can_rxdata_frame.cmd);
hi1000 18:e6ed582f7022 121 handleJamTrackCommand(can_rxdata_frame);
hi1000 18:e6ed582f7022 122 break;
hi1000 18:e6ed582f7022 123 case TeaTrack:
hi1000 18:e6ed582f7022 124 printf("TeaTrack command: %d \r\n", can_rxdata_frame.cmd);
hi1000 18:e6ed582f7022 125 handleTeaTrackCommand(can_rxdata_frame);
hi1000 18:e6ed582f7022 126 break;
hi1000 18:e6ed582f7022 127 case Tea:
hi1000 18:e6ed582f7022 128 printf("Tea command: %d \r\n", can_rxdata_frame.cmd);
hi1000 18:e6ed582f7022 129 handleTeaCommand(can_rxdata_frame);
hi1000 18:e6ed582f7022 130 break;
hi1000 18:e6ed582f7022 131 case Jam:
hi1000 18:e6ed582f7022 132 printf("Jam command: %d \r\n", can_rxdata_frame.cmd);
hi1000 18:e6ed582f7022 133 handleJamCommand(can_rxdata_frame);
hi1000 18:e6ed582f7022 134 break;
hi1000 18:e6ed582f7022 135 case Shaker:
hi1000 18:e6ed582f7022 136 printf("Shaker command: %d \r\n", can_rxdata_frame.cmd);
hi1000 18:e6ed582f7022 137 handleShakerCommand(can_rxdata_frame);
hi1000 18:e6ed582f7022 138 break;
hi1000 17:faa4d4976d22 139 }
hi1000 18:e6ed582f7022 140
hi1000 8:6105ffbaf237 141 }
hi1000 8:6105ffbaf237 142 }
hi1000 8:6105ffbaf237 143
hi1000 7:e0c7e624c5fa 144
hi1000 7:e0c7e624c5fa 145 }