40 #define STACKSIZE_TASK 256 41 #define PRIORITY_TASK 2 43 #define MAX_BLOCK_COUNT 3 47 #define SWITCH ((volatile unsigned char*)(0x6C000400)) 74 static const message_t msg_status_request = {0,1,{1}};
75 static const uint16_t msg_status_response_id = 1;
76 static const message_t msg_cmd_start = {2,3,{1,0,0}};
77 static const message_t msg_cmd_stop = {2,3,{2,0,0}};
78 static const message_t msg_cmd_stoppos = {2,3,{3,0x00,0xB6}};
79 static const message_t msg_cmd_done= {2,3,{4,0,0}};
80 static const message_t msg_cmd_reset= {0xF,0};
83 static const message_t msg_cmd_disp_initial_pos = {0x142, 3, {1, 0, 100}};
84 static const message_t msg_cmd_disp_start_right = {0x142, 3, {1, 0xE4, 100}};
85 static const message_t msg_cmd_disp_move_right = {0x142, 3, {1, 0x32, 50}};
86 static const message_t msg_cmd_disp_start_left = {0x142, 3, {1, 0x1C, 100}};
87 static const message_t msg_cmd_disp_move_left = {0x142, 3, {1, 0xCE, 50}};
90 static QueueHandle_t ucan_queue_mid;
91 static QueueHandle_t ucan_queue_left;
92 static QueueHandle_t ucan_queue_right;
95 static SemaphoreHandle_t bcs_left_start_semaphore;
96 static SemaphoreHandle_t bcs_mid_start_semaphore;
97 static SemaphoreHandle_t bcs_right_start_semaphore;
98 static SemaphoreHandle_t bcs_left_free_semaphore;
99 static SemaphoreHandle_t bcs_mid_free_semaphore;
100 static SemaphoreHandle_t bcs_right_free_semaphore;
102 static QueueHandle_t bcs_left_end_queue;
103 static QueueHandle_t bcs_right_end_queue;
114 static void bcs_send_msg(
const message_t* msg, uint16_t baseaddr)
129 static status_t* bcs_await_block(
enum belt_select belt, QueueHandle_t ucan_queue, CARME_CAN_MESSAGE* tmp_message)
132 uint16_t wait_count = 0;
139 bcs_send_msg(&msg_status_request,belt);
143 if(xQueueReceive(ucan_queue,tmp_message,4000)==pdFALSE) {
145 display_log(statR,
"Waiting on block (%u): timeout", wait_count);
147 }
while(tmp_message->id != belt+msg_status_response_id);
149 status = (
status_t*)&(tmp_message->data);
153 if(status->detection == 3) {
154 display_log(statR,
"Waiting on block. Found! position %04x location %d", status->position, status->location );
158 display_log(statR,
"Waiting on block (%u): detection: %u pos: %04x",wait_count,status->detection, status->position);
162 if(wait_count >= 100) {
163 bcs_send_msg(&msg_cmd_done,belt);
164 display_log(statR,
"Waiting on block (%u): Aborted",wait_count);
183 xSemaphoreTake(bcs_left_free_semaphore,portMAX_DELAY);
186 xSemaphoreTake(bcs_right_free_semaphore,portMAX_DELAY);
189 xSemaphoreTake(bcs_mid_free_semaphore,portMAX_DELAY);
205 xSemaphoreGive(bcs_left_start_semaphore);
208 xSemaphoreGive(bcs_right_start_semaphore);
211 xSemaphoreGive(bcs_mid_start_semaphore);
227 xSemaphoreGive(bcs_left_free_semaphore);
230 xSemaphoreGive(bcs_right_free_semaphore);
233 xSemaphoreGive(bcs_mid_free_semaphore);
246 static void bcs_await_drop(
enum belt_select belt,
bool allow_skip)
251 xSemaphoreTake(bcs_left_start_semaphore,portMAX_DELAY);
254 xSemaphoreTake(bcs_right_start_semaphore,portMAX_DELAY);
258 xSemaphoreTake(bcs_mid_start_semaphore,2000);
261 xSemaphoreTake(bcs_mid_start_semaphore,portMAX_DELAY);
280 xQueueReceive(bcs_left_end_queue,&pos,portMAX_DELAY);
282 xQueueReceive(bcs_right_end_queue,&pos,portMAX_DELAY);
293 static void bcs_task(
void *pv_data)
297 QueueHandle_t ucan_queue;
301 ucan_queue = ucan_queue_left;
304 ucan_queue = ucan_queue_right;
307 ucan_queue = ucan_queue_mid;
314 bool move_left =
true;
315 uint8_t mid_start_without_mutex_count = 0;
320 bcs_send_msg(&msg_cmd_reset,belt);
324 bool allow_skip =
false;
327 mid_start_without_mutex_count++;
332 bcs_send_msg(&msg_cmd_disp_initial_pos,0);
335 bcs_await_drop(belt,allow_skip);
339 bcs_send_msg(&msg_cmd_start,belt);
340 bcs_send_msg(&msg_cmd_stoppos,belt);
343 CARME_CAN_MESSAGE tmp_message;
344 status_t* status = bcs_await_block(belt,ucan_queue,&tmp_message);
356 bcs_send_msg(move_left ? &msg_cmd_disp_start_left : &msg_cmd_disp_start_right,0);
365 xQueueSend(bcs_left_end_queue,&(status->location),portMAX_DELAY);
369 xQueueSend(bcs_right_end_queue,&(status->location),portMAX_DELAY);
375 bcs_send_msg(move_left ? &msg_cmd_disp_move_left : &msg_cmd_disp_move_right,0);
379 move_left = ! move_left;
386 bcs_send_msg(&msg_cmd_done,belt);
400 bcs_left_start_semaphore = xSemaphoreCreateBinary();
401 bcs_right_start_semaphore = xSemaphoreCreateBinary();
402 bcs_mid_start_semaphore = xSemaphoreCreateBinary();
404 bcs_left_free_semaphore = xSemaphoreCreateBinary();
405 bcs_mid_free_semaphore = xSemaphoreCreateBinary();
406 bcs_right_free_semaphore = xSemaphoreCreateBinary();
407 xSemaphoreGive(bcs_left_free_semaphore);
409 xSemaphoreGive(bcs_right_free_semaphore);
411 bcs_left_end_queue = xQueueCreate(1,
sizeof(int8_t));
412 bcs_right_end_queue = xQueueCreate(1,
sizeof(int8_t));
418 ucan_queue_right = xQueueCreate(1,
sizeof(CARME_CAN_MESSAGE));
419 ucan_queue_left = xQueueCreate(1,
sizeof(CARME_CAN_MESSAGE));
420 ucan_queue_mid = xQueueCreate(1,
sizeof(CARME_CAN_MESSAGE));
void bcs_init()
Initializes the belt conveyer system and starts the belt tasks global.
#define MAX_BLOCK_COUNT
Number of blocks to work with. Must be between 2 and 4.
void bcs_signal_band_free(enum belt_select belt)
Signal that a block has been removed from a belt and the belt is free again global.
uint8_t length
Data-Length of the CAN message.
void bcs_signal_dropped(enum belt_select belt)
Signal that a block has been dropped on a belt global.
bool ucan_send_data(uint8_t n_data_bytes, uint16_t msg_id, const uint8_t *data)
Send data to the can output message queue global.
#define STACKSIZE_TASK
Stack size of all bcs tasks.
void bcs_prepare_drop(enum belt_select belt)
Prepares a block drop operation to a specific belt global.
uint8_t display_log(uint8_t id, const char *fmtstr,...)
Logs a message to the display global.
int8_t bcs_grab(enum belt_select belt)
Instructs the system that we want to grab a block from the bcs global.
bool ucan_link_message_to_queue_mask(uint16_t mask, uint16_t message_id, QueueHandle_t queue)
Set a message mask to map multiple message to a queue global.
belt_select
The belt_select enum differenciates between the different belt tasks. The members point to the base a...
uint16_t subid
Id offset of the base-id.
BCS can message structure.
#define PRIORITY_TASK
Priority of all bcs tasks.
struct __attribute__((__packed__))
Status message received by belt conveyer system.