Compare commits

...

7 Commits

Author SHA1 Message Date
Miaow
77dfccba79 feat: 添加队列空提示
1. 当存储上位机的图像数据队列为空时,将会给上位机发送报错信息
2. README中添加了喷阀工作原理
3. 添加了部署步骤说明doc/deploy.md
2023-02-08 20:53:53 +08:00
Miaow
5e6b280a49 perf: 修改PCB工程
PCB工程中添加了outjob
2023-02-05 23:18:25 +08:00
Miaow
ceb5469c17 docs: 修改README,添加version文件
1. README中添加了实物图片
2. 修正了README的目录结构章节
3. README中扩充了tag定义
4. README中添加了version文件的要求
5. 各目录下添加了version文件
6. 添加了doc
2023-02-05 22:10:16 +08:00
MIAOW
61bc985d5c
Merge pull request #1 from weiliang987644015/b01-pcandysorting-thardcandy1
add epc9600ioextend board
2022-06-13 16:05:47 +08:00
weiliang987644015
a6547e4184 add epc9600ioextend board 2022-06-13 16:02:47 +08:00
Miaow
d6e22519d5 fix bug at resuming operation
The START command following a STOP command can cause misplaced counters. This commit fixes the bug by setting counter variables to their initial value as soon as STOP command reaches.
2022-03-10 15:30:21 +08:00
miaow
657cdf2691 add hardware and script readme
Co-Authored-By: Suhandong <80463329+Suhandong@users.noreply.github.com>
2022-01-16 14:06:42 +08:00
32 changed files with 1708 additions and 102 deletions

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 86 KiB

BIN
README.assets/qwe.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 MiB

View File

@ -1,15 +1,44 @@
# 下位机 # 下位机
下位机根据传送带脉冲等触发相机,接收上位机给的数据,按其要求控制阀板,用在各种分选机上。 糖果分选机的下位机根据传送带脉冲等触发相机,接收上位机给的数据,按其要求控制阀板,用在各种分选机上。本次下位机采用的硬件是[广州致远电子股份有限公司 ](https://www.zlg.cn/)(周立功)的[EPC-9600I-L工控主板](https://www.zlg.cn/ipc/ipc/product/id/77.html)简称EPC9600自带各种接口但是除了网口和DI/DO接口其他接口统统用不到。阀板接口、触发相机的接口、编码器接口都是连接自己做的IO扩展板将IO扩展板插在底板DI/DO接口上叠起来因此称为叠板。
![qwe](README.assets/qwe.png)
图中左到右分别为EPC9600IO扩展板糖果分选机整机相机上位机编码器。其中相机的触发线、编码器连接到IO扩展板相机、上位机、EPC9600连接到以太网交换机。
IO扩展板提供了
- 1个12V电源输入
- 2个相机触发1个为冗余
- 2个编码器输入1个为冗余
- 4个阀板接口3个为冗余
要注意IO扩展板上的供电接口仅仅用于IO扩展板自身的供电EPC9600的供电需要另外提供。<br>下面是接线图:
![接线图待补充](README.assets/接线图.png)
## 目录结构 ## 目录结构
- binary为编译好的可执行文件 - doc为说明文档包括开发和部署细节、硬件设计的描述等
- [deploy.md](./doc/deploy.md)为开发和部署说明,首先看这个文档
- [hardware_description.md](./doc/hardware_description.md)为PCB设计说明
- script为配置系统、安装环境、安装可执行文件、卸载可执行文件等的脚本 - script为配置系统、安装环境、安装可执行文件、卸载可执行文件等的脚本
- protocol为上位机和下位机通信的协议 - protocol为上位机和下位机通信的协议
- hardware下位机主板、接口板、底板等的硬件设计 - hardware下位机主板、接口板、底板等的硬件设计
- source为可执行文件的源程序 - source为可执行文件的源程序
## 喷阀原理
喷嘴由两片CNC加工的金属工件拼合而成每一片工件上有48个深度为喷嘴宽度的一半的沟槽两片拼合形成截面为矩形的48个喷嘴通过软管连接到喷阀的气流出口。打开相应喷阀就会在喷嘴处射出气流。喷嘴以喷出气流向下的方向安装整体平行于传送带滚筒在传送带末端上方。如图
<img src="README.assets/20230208200017.jpg" alt="20230208200017" style="zoom: 50%;" />
工作时射出的气流将瑕疵糖果向下吹走,正常糖果被传送带喷出,如下视频
<video src="README.assets/144262e7fd3707720962e083d5304a7e.mp4"></video>
## 版本 ## 版本
由于经常有不同类型的新要求出现,比如分选糖果、分选烟梗、同为糖果也具有不同的参数,因此不同的下位机型号(注意不是更新,比如同一台机器需要设置新的参数)应建立不同的分支,**主分支无实际意义** 由于经常有不同类型的新要求出现,比如分选糖果、分选烟梗、同为糖果也具有不同的参数,因此不同的下位机型号(注意不是更新,比如同一台机器需要设置新的参数)应建立不同的分支,**主分支无实际意义**
@ -27,10 +56,10 @@ b分支编号-p生产环境项目名-t分选对象[-其他特点1[-其他特点2
版本号遵循定义如下(不使用中文,小写无空格) 版本号遵循定义如下(不使用中文,小写无空格)
```shell ```shell
b分支编号-n编译号-h硬件版本-p协议版本-s脚本版本-r代码版本 b分支编号[-d文档版本]-hPCB设计版本[-lFPGA设计版本]-p协议版本[-s脚本版本]-aAPP代码版本[-i驱动版本][-c系统编译配置版本][-e设备树版本]
``` ```
分支编号和分支命名中编号一致 中括号在这里表示可省略的项,中括号本身不应出现在实际命名中。分支编号和分支命名中编号一致。各部分版本应在相应目录下创建文件注明,比如`2.1`版本的PCB设计PCB工程目录中建立`version`文本文件,内容为文本`2.1`。对于涉及整体设计的大改动版本号加`1`,其余改动版本号加`0.1`。每个分支的版本各自独立,新分支的版本从`1.0`起计算
## 作者 ## 作者

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 442 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 419 KiB

67
doc/deploy.md Normal file
View File

@ -0,0 +1,67 @@
# 下位机的部署步骤
按下面标题的顺序进行,不要轻易跳过步骤,除非你知道你在做什么
## 硬件连接
### 启动方式选择
EPC-9600I-L支持从NAND或者SD卡启动通过 S7拨码开关控制拨码开关如图全部设置为OFF表示从NAND启动。
<img src="deploy.assets/5e6af30fa23d9.jpg" alt="5e6af30fa23d9" style="zoom:33%;" />
### IO和供电连接
完成以下步骤后,应如下图所示
1. 将IO扩展板插入J1牛角座
2. 按接线图连接相机触发线、编码器的任一相、阀板
3. 按接线图连接IO扩展板的电源
4. 按接线图连接EPC9600的电源
5. 按接线图连接网线到交换机
6. 连接调试用电脑到交换机
<img src="deploy.assets/20230208183606.jpg" alt="20230208183606" style="zoom: 33%;" />
启动交换机、EPC9600、IO扩展板的电源
## 软件配置
### 登录和配置
断开并禁用电脑连接的其他网络和网络适配器包括无线网将电脑连接EPC9600的网口设置到192.168.1.0/24网络中
SSH登录账号`root`,密码`root`<br>出厂默认IP为`192.168.1.136`
可以使用任何SSH软件进行登录推荐`Xshell`连接后执行下面的命令设置新的时间和IP
```shell
[root@epc-9600 ~]# date 2023.02.08-15:34:00
[root@epc-9600 ~]# hwclock -w
[root@epc-9600 ~]# eeprom net set ip 192.168.2.9
```
时间不要照抄上面的命令设置为当前时间即可IP必须为`192.168.2.9`,执行下面命令重启
```shell
[root@epc-9600 ~]# reboot
```
### 安装下位机软件
因为设置了新的IP所以将电脑连接EPC9600的网口设置为`192.168.2.11/24`重新SSH连接IP为`192.168.2.9`的EPC9600。同时启动SFTP软件推荐`Xftp`,将[script/S90target](../script/S90target)传输到EPC9600的`/tmp`中,运行下面命令配置下位机软件自启动:
```shell
[root@epc-9600 ~]# wr mv /tmp/S90target /etc/init.d/
[root@epc-9600 ~]# wr chmod 755 /etc/init.d/S90target
```
用相同的放将本仓库release中`app.zip`解压出的下位机软件`target`传输到EPC9600的`/tmp`中,运行下面命令安装下位机软件:
```shell
[root@epc-9600 ~]# wr mv /tmp/target /root
[root@epc-9600 ~]# wr chmod 755 /root/target
[root@epc-9600 ~]# sync
```

View File

1
doc/version Normal file
View File

@ -0,0 +1 @@
0.2

297
hardware/Job1.OutJob Normal file

File diff suppressed because one or more lines are too long

BIN
hardware/asm/PCB1.PCBDwf Normal file

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1 @@
Record=TopLevelDocument|FileName=Sheet1.SchDoc|SheetNumber=1

BIN
hardware/lib/mw.PcbLib Normal file

Binary file not shown.

BIN
hardware/lib/mw.SchLib Normal file

Binary file not shown.

BIN
hardware/pcb/PCB1.PcbDoc Normal file

Binary file not shown.

BIN
hardware/sch/Sheet1.SchDoc Normal file

Binary file not shown.

1
hardware/version Normal file
View File

@ -0,0 +1 @@
1.1

1
protocol/version Normal file
View File

@ -0,0 +1 @@
1.0

2
script/S90target Normal file
View File

@ -0,0 +1,2 @@
#!/bin/sh
/root/target &

1
script/version Normal file
View File

@ -0,0 +1 @@
1.0

View File

@ -4,10 +4,10 @@
"name": "ARM", "name": "ARM",
"includePath": [ "includePath": [
"${workspaceFolder}/**", "${workspaceFolder}/**",
"/home/miaow/software/arm-2011.03/lib/gcc/arm-none-linux-gnueabi/4.5.2/include/**" "/home/miaow/software/gcc-arm-2011.03/lib/gcc/arm-none-linux-gnueabi/4.5.2/include/**"
], ],
"defines": [], "defines": [],
"compilerPath": "/home/miaow/software/arm-2011.03/bin/arm-none-linux-gnueabi-gcc", "compilerPath": "/home/miaow/software/gcc-arm-2011.03/bin/arm-none-linux-gnueabi-gcc",
"cStandard": "gnu99", "cStandard": "gnu99",
"cppStandard": "gnu++17", "cppStandard": "gnu++17",
"intelliSenseMode": "linux-gcc-arm", "intelliSenseMode": "linux-gcc-arm",
@ -15,7 +15,7 @@
"browse": { "browse": {
"path": [ "path": [
"${workspaceFolder}", "${workspaceFolder}",
"/home/miaow/software/arm-2011.03/lib/gcc/arm-none-linux-gnueabi/4.5.2/include" "/home/miaow/software/gcc-arm-2011.03/lib/gcc/arm-none-linux-gnueabi/4.5.2/include"
] ]
} }
} }

View File

@ -16,7 +16,9 @@
"queue.h": "c", "queue.h": "c",
"stdio.h": "c", "stdio.h": "c",
"encoder.h": "c", "encoder.h": "c",
"semaphore.h": "c" "semaphore.h": "c",
"socket.h": "c",
"host_computer.h": "c"
}, },
"makefile.launchConfigurations": [ "makefile.launchConfigurations": [
{ {

View File

@ -1,5 +1,5 @@
#makefile for file_ioctl #makefile for file_ioctl
CROSS_COMPILE ?= /home/miaow/software/arm-2011.03/bin/arm-none-linux-gnueabi- CROSS_COMPILE ?= /home/miaow/software/gcc-arm-2011.03/bin/arm-none-linux-gnueabi-
TARGET := target TARGET := target
BUILD_DIR := build BUILD_DIR := build

View File

@ -1,3 +1,14 @@
/**
* @file gpio_common.h
* @brief Operate the GPIO port of Zhou Ligong linux industrial control board
* @details is_file_exist(const char *file_path) determine whether the specified file exists
* print_array(int *array, int count) used to print out the value of the queue buffer, easy to debug and use
* @mainpage github.com/NanjingForestryUniversity
* @author miaow
* @email 3703781@qq.com
* @version v0.9.0
* @date 2021/12/25 merry christmas
*/
#ifndef __GPIO_COMMON_H #ifndef __GPIO_COMMON_H
#define __GPIO_COMMON_H #define __GPIO_COMMON_H

View File

@ -1,9 +1,9 @@
/** /**
* @file host_computer.c * @file host_computer.c
* @brief Commnunicate with host computer. Protocal is described in hostcomputer通信协.md * @brief Commnunicate with host computer. Protocal is described in .md
* @author miaow (3703781@qq.com) * @author miaow (3703781@qq.com)
* @version 1.0 * @version 1.1
* @date 2022/01/16 * @date 2023/02/08
* *
* @copyright Copyright (c) 2022 miaow * @copyright Copyright (c) 2022 miaow
* *
@ -11,6 +11,7 @@
* <table> * <table>
* <tr><th>Date <th>Version <th>Author <th>Description * <tr><th>Date <th>Version <th>Author <th>Description
* <tr><td>2022/01/16 <td>1.0 <td>miaow <td>Write this file * <tr><td>2022/01/16 <td>1.0 <td>miaow <td>Write this file
* <tr><td>2023/02/08 <td>1.1 <td>miaow <td>Add fifo empty detection
* </table> * </table>
*/ */
#include <host_computer.h> #include <host_computer.h>
@ -84,6 +85,16 @@ static int recvn(int fd, char *buf, int size)
return size; return size;
} }
/**
* @brief Send string "error"
* @param fd The file descriptor that receives error
*/
static void send_error(int fd)
{
write(fd, "error", 5);
printf("error sent\r\n");
}
/** /**
* @brief To inspect the status of TCP connection * @brief To inspect the status of TCP connection
* @param sock_fd The socket * @param sock_fd The socket
@ -105,7 +116,7 @@ static int is_connected(int sock_fd)
void *loop_thread_func(void *param) void *loop_thread_func(void *param)
{ {
printf("loop thread in %s start\r\n", __FILE__); printf("loop thread in %s start\r\n", __FILE__);
int need_exit = 0; int need_exit = 0, error_sent = 0;
char pre; char pre;
uint16_t n_bytes; uint16_t n_bytes;
char type[2]; char type[2];
@ -143,39 +154,32 @@ void *loop_thread_func(void *param)
if (recvn(_global_structure.socket_fd, (char *)&pre, 1) > 1) if (recvn(_global_structure.socket_fd, (char *)&pre, 1) > 1)
{ {
// close(_global_structure.socket_fd);
printf("pre_len!=1\r\n"); printf("pre_len!=1\r\n");
continue; continue;
} }
if (pre != 0xAA) if (pre != 0xAA)
{ {
// close(_global_structure.socket_fd);
// printf("%X ", (int)pre);
fflush(stdout); fflush(stdout);
continue; continue;
} }
if (recvn(_global_structure.socket_fd, (char *)&n_bytes, 2) != 2) if (recvn(_global_structure.socket_fd, (char *)&n_bytes, 2) != 2)
{ {
// close(_global_structure.socket_fd);
printf("n_bytes_len!=2\r\n"); printf("n_bytes_len!=2\r\n");
continue; continue;
} }
n_bytes = ntohs(n_bytes); n_bytes = ntohs(n_bytes);
if (n_bytes > 4096 || n_bytes < 2) if (n_bytes > 4096 || n_bytes < 2)
{ {
// close(_global_structure.socket_fd);
printf("n_bytes>4096 or n_bytes<2\r\n"); printf("n_bytes>4096 or n_bytes<2\r\n");
continue; continue;
} }
if (recvn(_global_structure.socket_fd, (char *)type, 2) != 2) if (recvn(_global_structure.socket_fd, (char *)type, 2) != 2)
{ {
// close(_global_structure.socket_fd);
printf("type!=2\r\n"); printf("type!=2\r\n");
continue; continue;
} }
if (recvn(_global_structure.socket_fd, (char *)data, n_bytes - 2) != n_bytes - 2) if (recvn(_global_structure.socket_fd, (char *)data, n_bytes - 2) != n_bytes - 2)
{ {
// close(_global_structure.socket_fd);
printf("data_len!=n_bytes-2\r\n"); printf("data_len!=n_bytes-2\r\n");
continue; continue;
} }
@ -183,19 +187,16 @@ void *loop_thread_func(void *param)
data[n_bytes - 2] = 0; data[n_bytes - 2] = 0;
if (recvn(_global_structure.socket_fd, (char *)check, 2) != 2) if (recvn(_global_structure.socket_fd, (char *)check, 2) != 2)
{ {
// close(_global_structure.socket_fd);
printf("check_len!=2\r\n"); printf("check_len!=2\r\n");
continue; continue;
} }
if (recvn(_global_structure.socket_fd, (char *)&pre, 1) != 1) if (recvn(_global_structure.socket_fd, (char *)&pre, 1) != 1)
{ {
// close(_global_structure.socket_fd);
printf("end_len!=1\r\n"); printf("end_len!=1\r\n");
continue; continue;
} }
if (pre != 0xBB) if (pre != 0xBB)
{ {
// close(_global_structure.socket_fd);
printf("end!=0xBB\r\n"); printf("end!=0xBB\r\n");
continue; continue;
} }
@ -205,9 +206,9 @@ void *loop_thread_func(void *param)
if (type[0] == 'd' && type[1] == 'a') if (type[0] == 'd' && type[1] == 'a')
{ {
// printf("%dbytes of data put to data queue\r\n", (int)n_bytes - 2); // printf("%dbytes of data put to data queue\r\n", (int)n_bytes - 2);
if (n_bytes - 2 != 6 * HOST_COMPUTER_PICTURE_ROW_NUM) if (n_bytes - 2 != HOST_COMPUTER_PICTURE_BYTES)
{ {
printf("n_bytes-2!=%d\r\n", 6 * HOST_COMPUTER_PICTURE_ROW_NUM); printf("n_bytes-2!=%d\r\n", HOST_COMPUTER_PICTURE_BYTES);
continue; continue;
} }
int data_index = 0; int data_index = 0;
@ -220,17 +221,24 @@ void *loop_thread_func(void *param)
for (int i = 0; i < HOST_COMPUTER_PICTURE_ROW_NUM; i++) for (int i = 0; i < HOST_COMPUTER_PICTURE_ROW_NUM; i++)
{ {
tmp_one_line_data = 0ul; tmp_one_line_data = 0ul;
for (int j = 0; j < 6; j++) for (int j = 0; j < HOST_COMPUTER_PICTURE_COLUMN_BYTES; j++)
{ {
tmp_one_line_data <<= 8; tmp_one_line_data <<= 8;
tmp_one_line_data |= data[data_index++]; tmp_one_line_data |= data[data_index++];
} }
queue_uint64_put(_global_structure.data_q, tmp_one_line_data); queue_uint64_put(_global_structure.data_q, tmp_one_line_data);
} }
// printf("queue_num=%d\r\n", _global_structure.data_q->nData);
if (_global_structure.data_q->nData == HOST_COMPUTER_PICTURE_ROW_NUM && !error_sent)
{
error_sent = 1;
send_error(_global_structure.socket_fd);
}
} }
else if (type[0] == 's' && type[1] == 't') else if (type[0] == 's' && type[1] == 't')
{ {
// printf("Start put to cmd queue, param:%d\r\n", (int)atoll(data)); // printf("Start put to cmd queue, param:%d\r\n", (int)atoll(data));
error_sent = 0;
queue_uint64_put(_global_structure.cmd_q, (atoll(data) << 32) | HOSTCOMPUTER_CMD_START); queue_uint64_put(_global_structure.cmd_q, (atoll(data) << 32) | HOSTCOMPUTER_CMD_START);
} }
else if (type[0] == 's' && type[1] == 'p') else if (type[0] == 's' && type[1] == 'p')

View File

@ -1,9 +1,9 @@
/** /**
* @file host_computer.h * @file host_computer.h
* @brief Commnunicate with host computer. Protocal is described in hostcomputer通信协.md * @brief Commnunicate with host computer. Protocal is described in .md
* @author miaow (3703781@qq.com) * @author miaow (3703781@qq.com)
* @version 1.0 * @version 1.1
* @date 2022/01/16 * @date 2023/02/08
* *
* @copyright Copyright (c) 2022 miaow * @copyright Copyright (c) 2022 miaow
* *
@ -11,6 +11,7 @@
* <table> * <table>
* <tr><th>Date <th>Version <th>Author <th>Description * <tr><th>Date <th>Version <th>Author <th>Description
* <tr><td>2022/01/16 <td>1.0 <td>miaow <td>Write this file * <tr><td>2022/01/16 <td>1.0 <td>miaow <td>Write this file
* <tr><td>2023/02/08 <td>1.1 <td>miaow <td>Add 3 macros for picture from host computer
* </table> * </table>
*/ */
#ifndef __HOST_COMPUTER_H #ifndef __HOST_COMPUTER_H
@ -23,6 +24,9 @@
#define HOST_COMPUTER_IP "192.168.2.10" #define HOST_COMPUTER_IP "192.168.2.10"
#define HOST_COMPUTER_PORT 13452 #define HOST_COMPUTER_PORT 13452
#define HOST_COMPUTER_PICTURE_ROW_NUM 500 #define HOST_COMPUTER_PICTURE_ROW_NUM 500
#define HOST_COMPUTER_PICTURE_COLUMN_NUM 48
#define HOST_COMPUTER_PICTURE_COLUMN_BYTES (HOST_COMPUTER_PICTURE_COLUMN_NUM / 8)
#define HOST_COMPUTER_PICTURE_BYTES (HOST_COMPUTER_PICTURE_COLUMN_BYTES * HOST_COMPUTER_PICTURE_ROW_NUM)
/** /**
* @brief The commonds, ref hostcomputer通信协议.md * @brief The commonds, ref hostcomputer通信协议.md

View File

@ -1,7 +1,25 @@
/**
* @file main.c
* @brief Excuting commands, resample data, triggle camera, and send data to valve
* @author miaow (3703781@qq.com)
* @version 1.1
* @date 2023/02/08
*
* @copyright Copyright (c) 2022 miaow
*
* @par Changelog:
* <table>
* <tr><th>Date <th>Version <th>Author <th>Description
* <tr><td>2022/01/16 <td>1.0 <td>miaow <td>Write this file
* <tr><td>2023/02/08 <td>1.1 <td>miaow <td>Add debug option for interval of camera triggle
* </table>
*/
#include <valve.h> #include <valve.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <sys/time.h>
#include <math.h> #include <math.h>
#include <main.h>
#include <queue_uint64.h> #include <queue_uint64.h>
#include <camera_trigger.h> #include <camera_trigger.h>
#include <encoder.h> #include <encoder.h>
@ -12,11 +30,8 @@
*/ */
typedef enum typedef enum
{ {
NOT_INITIALIZED = 0, SLEEPING = 0,
INITIALIZED = 1, RUNNING = 1,
RUNNING = 2,
SLEEPING = 3,
STOPPED = 4
} status_enum_t; } status_enum_t;
valvedata_t valvedata = {0}; valvedata_t valvedata = {0};
@ -25,11 +40,16 @@ queue_uint64_msg_t cmd_queue = {0};
static int count_valve = 1, count_camera = 0, count_valve_should_be = 2; static int count_valve = 1, count_camera = 0, count_valve_should_be = 2;
static uint64_t count_continues = 0UL, count_valve_continues = 0UL, count_camera_continues = 0UL; static uint64_t count_continues = 0UL, count_valve_continues = 0UL, count_camera_continues = 0UL;
static status_enum_t status = NOT_INITIALIZED; static status_enum_t status = SLEEPING;
static int camera_trigger_pulse_count = 0; static int camera_trigger_pulse_count = 500;
static int valve_should_trigger_pulse_count = 0; static int valve_should_trigger_pulse_count = 1;
static int valve_trigger_pulse_count = 0; static int valve_trigger_pulse_count = 10;
static int camera_to_valve_pulse_count = 0; static int camera_to_valve_pulse_count = 3015;
#if defined(DEBUG_CAMERA_TRIG_PERIOD)
static struct timeval tv;
static uint64_t camera_period_interval_last_us = 0UL;
static uint64_t camera_period_interval_us = 0UL;
#endif
#define ROTATE_UINT64_RIGHT(x, n) ((x) >> (n)) | ((x) << ((64) - (n))) #define ROTATE_UINT64_RIGHT(x, n) ((x) >> (n)) | ((x) << ((64) - (n)))
#define ROTATE_UINT64_LEFT(x, n) ((x) << (n)) | ((x) >> ((64) - (n))) #define ROTATE_UINT64_LEFT(x, n) ((x) << (n)) | ((x) >> ((64) - (n)))
@ -50,11 +70,15 @@ int main(int argc, char *argv[])
// fflush(stdout); // fflush(stdout);
// valve_test3(100.0f); // valve_test3(100.0f);
// valve_test2(200.0f, 0); // valve_test2(200.0f, 0);
// for (int i = 0; i < 999; i++)
// {
// valve_test(200.0f); // valve_test(200.0f);
// }
// printf("OK\r\n"); // printf("OK\r\n");
// valve_deinit(); // valve_deinit();
hostcomputer_init(&data_queue, &cmd_queue); hostcomputer_init(&data_queue, &cmd_queue);
printf("\r\n>>>>>\r\nstatus==SLEEPING\r\n<<<<<\r\n\r\n");
uint64_t cmd; uint64_t cmd;
int TRUE = 1; int TRUE = 1;
while (TRUE) while (TRUE)
@ -62,8 +86,8 @@ int main(int argc, char *argv[])
if (queue_uint64_get(&cmd_queue, &cmd) == 0) if (queue_uint64_get(&cmd_queue, &cmd) == 0)
{ {
process_cmd(&cmd); process_cmd(&cmd);
usleep(100000);
} }
usleep(100000);
} }
hostcomputer_deinit(); hostcomputer_deinit();
queue_uint64_deinit(&data_queue); queue_uint64_deinit(&data_queue);
@ -79,14 +103,16 @@ void process_cmd(uint64_t *cmd)
{ {
if (tmp_cmd == HOSTCOMPUTER_CMD_START) if (tmp_cmd == HOSTCOMPUTER_CMD_START)
{ {
queue_uint64_clear(&data_queue);
valve_should_trigger_pulse_count = camera_trigger_pulse_count / HOST_COMPUTER_PICTURE_ROW_NUM; valve_should_trigger_pulse_count = camera_trigger_pulse_count / HOST_COMPUTER_PICTURE_ROW_NUM;
for (int i = 0; i < camera_to_valve_pulse_count * HOST_COMPUTER_PICTURE_ROW_NUM / camera_trigger_pulse_count; i++) for (int i = 0; i < camera_to_valve_pulse_count * HOST_COMPUTER_PICTURE_ROW_NUM / camera_trigger_pulse_count; i++)
queue_uint64_put(&data_queue, 0); queue_uint64_put(&data_queue, 0L);
valve_init(); valve_init();
cameratrigger_init(); cameratrigger_init();
encoder_init(on_encoder); encoder_init(on_encoder);
printf("\r\n>>>>>\r\nstatus==RUNNING\r\ncamera_trigger_pulse_count=%d\r\nvalve_trigger_pulse_count=%d\r\ncamera_to_valve_pulse_count=%d\r\n<<<<<\r\n\r\n", camera_trigger_pulse_count, valve_trigger_pulse_count, camera_to_valve_pulse_count);
status = RUNNING; status = RUNNING;
printf("\r\n>>>>>\r\nstatus==RUNNING\r\n<<<<<\r\n\r\n");
} }
else if (tmp_cmd == HOSTCOMPUTER_CMD_TEST) else if (tmp_cmd == HOSTCOMPUTER_CMD_TEST)
{ {
@ -94,10 +120,7 @@ void process_cmd(uint64_t *cmd)
valve_test(500.0f); valve_test(500.0f);
valve_deinit(); valve_deinit();
} }
} else if (tmp_cmd == HOSTCOMPUTER_CMD_SETCAMERATRIGPULSECOUNT)
else if (status == NOT_INITIALIZED)
{
if (tmp_cmd == HOSTCOMPUTER_CMD_SETCAMERATRIGPULSECOUNT)
{ {
camera_trigger_pulse_count = tmp_data; camera_trigger_pulse_count = tmp_data;
} }
@ -109,38 +132,6 @@ void process_cmd(uint64_t *cmd)
{ {
camera_to_valve_pulse_count = tmp_data; camera_to_valve_pulse_count = tmp_data;
} }
else if (tmp_cmd == HOSTCOMPUTER_CMD_TEST)
{
valve_init();
valve_test(500.0f);
valve_deinit();
}
if (camera_trigger_pulse_count != 0 && valve_trigger_pulse_count != 0 && camera_to_valve_pulse_count != 0)
{
status = INITIALIZED;
printf("\r\n>>>>>\r\nstatus==INITIALIZED\r\ncamera_trigger_pulse_count=%d\r\nvalve_trigger_pulse_count=%d\r\ncamera_to_valve_pulse_count=%d\r\n<<<<<\r\n\r\n", camera_trigger_pulse_count, valve_trigger_pulse_count, camera_to_valve_pulse_count);
}
}
else if (status == INITIALIZED)
{
if (tmp_cmd == HOSTCOMPUTER_CMD_START)
{
valve_should_trigger_pulse_count = camera_trigger_pulse_count / HOST_COMPUTER_PICTURE_ROW_NUM;
printf("valve_should_trigger_pulse_count=%d", valve_should_trigger_pulse_count);
for (int i = 0; i < camera_to_valve_pulse_count * HOST_COMPUTER_PICTURE_ROW_NUM / camera_trigger_pulse_count; i++)
queue_uint64_put(&data_queue, 0);
valve_init();
cameratrigger_init();
encoder_init(on_encoder);
status = RUNNING;
printf("\r\n>>>>>\r\nstatus==RUNNING\r\n<<<<<\r\n\r\n");
}
else if (tmp_cmd == HOSTCOMPUTER_CMD_TEST)
{
valve_init();
valve_test(500.0f);
valve_deinit();
}
} }
else if (status == RUNNING) else if (status == RUNNING)
{ {
@ -150,8 +141,21 @@ void process_cmd(uint64_t *cmd)
cameratrigger_deinit(); cameratrigger_deinit();
valve_deinit(); valve_deinit();
queue_uint64_clear(&data_queue); queue_uint64_clear(&data_queue);
status = SLEEPING; #if defined(DEBUG_CAMERA_TRIG_PERIOD)
printf("\r\n>>>>>\r\nstatus==SLEEPING\r\ncamera_period_us=%.2f\r\n<<<<<\r\n\r\n", (float)camera_period_interval_us / (float)(count_camera_continues - 1));
camera_period_interval_us = 0UL;
camera_period_interval_last_us = 0UL;
#else
printf("\r\n>>>>>\r\nstatus==SLEEPING\r\n<<<<<\r\n\r\n"); printf("\r\n>>>>>\r\nstatus==SLEEPING\r\n<<<<<\r\n\r\n");
#endif
count_continues = 0UL;
count_valve_continues = 0UL;
count_camera_continues = 0UL;
count_camera = 0;
count_valve = 1;
count_valve_should_be = 2;
status = SLEEPING;
} }
} }
} }
@ -159,7 +163,8 @@ void process_cmd(uint64_t *cmd)
void valve_test(float ms_for_each_channel) void valve_test(float ms_for_each_channel)
{ {
uint64_t valve_data = 1ul; uint64_t valve_data = 1ul;
for (int i = 0; i < 48; i++)
for (int i = 0; i < HOST_COMPUTER_PICTURE_COLUMN_NUM; i++)
{ {
usleep((useconds_t)(ms_for_each_channel * 500.0f)); usleep((useconds_t)(ms_for_each_channel * 500.0f));
valvedata.valvedata_1 = valve_data << i; valvedata.valvedata_1 = valve_data << i;
@ -212,33 +217,34 @@ void on_encoder()
{ {
count_continues++; count_continues++;
// send resampled data to valve, the resample cycle is valve_trigger_pulse_count
if (++count_valve == valve_trigger_pulse_count + 1) if (++count_valve == valve_trigger_pulse_count + 1)
{ {
count_valve = 1; count_valve = 1;
count_valve_continues++; count_valve_continues++;
valve_sendmsg(&valvedata); valve_sendmsg(&valvedata);
// printf("data:%llx send to valve, queue length is %d\r\n", valvedata.valvedata_1, data_queue.nData);
// printf("%016llx ", valvedata.valvedata_1);
fflush(stdout);
} }
// load valve data to valvedata structure
if (++count_valve_should_be == valve_should_trigger_pulse_count + 2) if (++count_valve_should_be == valve_should_trigger_pulse_count + 2)
{ {
count_valve_should_be = 2; count_valve_should_be = 2;
valvedata.valvedata_1 = 0; valvedata.valvedata_1 = 0;
queue_uint64_get(&data_queue, &(valvedata.valvedata_1)); queue_uint64_get(&data_queue, &(valvedata.valvedata_1));
// if (data_queue.nData == 0)
// {
// printf("sb\r\n");
// }
} }
// triggle camera in a cycle of camera_trigger_pulse_count
if (++count_camera == camera_trigger_pulse_count) if (++count_camera == camera_trigger_pulse_count)
{ {
// printf("camera triggled\r\n");
count_camera = 0; count_camera = 0;
count_camera_continues++; count_camera_continues++;
#if defined(DEBUG_CAMERA_TRIG_PERIOD)
gettimeofday(&tv, NULL);
if (camera_period_interval_last_us != 0UL)
camera_period_interval_us += ((uint64_t)tv.tv_sec * 1000000 + (uint64_t)tv.tv_usec) - camera_period_interval_last_us;
camera_period_interval_last_us = ((uint64_t)tv.tv_sec * 1000000 + (uint64_t)tv.tv_usec);
#endif
cameratrigger_trig(); cameratrigger_trig();
} }
} }

View File

@ -1,6 +1,23 @@
/**
* @file main.h
* @brief Excuting commands, resample data, triggle camera, and send data to valve
* @author miaow (3703781@qq.com)
* @version 1.1
* @date 2023/02/08
*
* @copyright Copyright (c) 2022 miaow
*
* @par Changelog:
* <table>
* <tr><th>Date <th>Version <th>Author <th>Description
* <tr><td>2022/01/16 <td>1.0 <td>miaow <td>Write this file
* <tr><td>2023/02/08 <td>1.1 <td>miaow <td>Add debug option for interval of camera triggle
* </table>
*/
#ifndef __MAIN_H #ifndef __MAIN_H
#define __MAIN_H #define __MAIN_H
// #define DEBUG_CAMERA_TRIG_PERIOD // uncomment this to print the interval of camera triggle in the unit of usecond
#endif #endif

View File

@ -1,3 +1,13 @@
/**
* @file valve.h
* @brief Operate the valveboard with Linux application
* @details Call valve_init() paired with valve_deinit() as their names imply, valve_send() can be executed several times to operate up to 6 valveboards between valve_init() and valve_deinit()
* @mainpage github.com/NanjingForestryUniversity
* @author miaow
* @email 3703781@qq.com
* @version v0.9.0
* @date 2021/12/25 merry christmas
*/
#ifndef __VALVE_INIT_H #ifndef __VALVE_INIT_H
#define __VALVE_INIT_H #define __VALVE_INIT_H
#include <gpio_common.h> #include <gpio_common.h>
@ -24,7 +34,6 @@ typedef struct
uint64_t valvedata_6; uint64_t valvedata_6;
} valvedata_t; } valvedata_t;
#define SCLK_FREQUENCE_KHZ 10000 #define SCLK_FREQUENCE_KHZ 10000
int valve_init(void); int valve_init(void);

1
source/version Normal file
View File

@ -0,0 +1 @@
1.1