The sample only provides the time synchronization sample code for the RTOS platform. Here is a simple example of monitoring the PPS signal on the Linux platform. The content is mainly to trigger printing by setting and monitoring the GPIO 12 node. Here, we print the current time when the PPS rising edge signal is detected (developers can refer to and modify it by themselves. During the porting process, it needs to be replaced with calling the function DjiTimeSync_TransferToAircraftTime to synchronize the aircraft time when the conditions are met)
In the PSDK code, it is recommended to implement it in a multi-threaded way (pthread_create) to avoid affecting the normal operation of the main thread.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <time.h>
#define GPIO_PIN "12" // GPIO12 引脚 / GPIO12 pin
// 函数:写入文件 / Function: Write to file
void write_to_file(const char *path, const char *value) {
int fd = open(path, O_WRONLY); // 打开文件以进行写入 / Open file for writing
if (fd == -1) {
perror("Failed to open file"); // 打开文件失败时打印错误 / Print error if failed to open file
exit(EXIT_FAILURE); // 退出程序 / Exit the program
}
write(fd, value, strlen(value)); // 写入值到文件 / Write value to file
close(fd); // 关闭文件描述符 / Close file descriptor
}
// 函数:读取文件 / Function: Read from file
int read_from_file(const char *path) {
char value[3]; // 存储读取的值 / Buffer to store the read value
int fd = open(path, O_RDONLY); // 打开文件以进行读取 / Open file for reading
if (fd == -1) {
perror("Failed to open file"); // 打开文件失败时打印错误 / Print error if failed to open file
exit(EXIT_FAILURE); // 退出程序 / Exit the program
}
read(fd, value, sizeof(value)); // 从文件读取值 / Read value from file
close(fd); // 关闭文件描述符 / Close file descriptor
return atoi(value); // 将读取的值转换为整数并返回 / Convert read value to integer and return
}
// 函数:获取当前时间 / Function: Print current time
void print_current_time() {
time_t now; // 当前时间变量 / Variable to store current time
time(&now); // 获取当前时间 / Get current time
struct tm *local = localtime(&now); // 转换为本地时间 / Convert to local time
printf("hello raspi - Current Time: %02d:%02d:%02d\n", // 打印当前时间 / Print current time
local->tm_hour, local->tm_min, local->tm_sec);
}
int main(void) {
// 导出 GPIO12 / Export GPIO12
write_to_file("/sys/class/gpio/export", GPIO_PIN);
usleep(100000); // 等待 GPIO 导出 / Wait for GPIO to be exported
// 设置 GPIO12 为输入 / Set GPIO12 as input
write_to_file("/sys/class/gpio/gpio12/direction", "in");
// 设置 GPIO12 为上拉输入 / Set GPIO12 as pull-up input
write_to_file("/sys/class/gpio/gpio12/edge", "rising");
printf("Listening for PPS signal on GPIO12...\n"); // 监听 GPIO12 上的 PPS 信号 / Listening for PPS signal on GPIO12...
// 循环监测 GPIO12 / Loop to monitor GPIO12
while (1) {
if (read_from_file("/sys/class/gpio/gpio12/value") == 1) { // 读取 GPIO12 的值 / Read value from GPIO12
print_current_time(); // 打印当前时间 / Print current time
usleep(1000000); // 避免多次重复触发,可以按需调整此延时 / Avoid multiple triggers, adjust this delay as needed
}
usleep(10000); // 等待 10 毫秒 / Wait for 10 milliseconds
}
// 清理 GPIO12 / Clean up GPIO12
write_to_file("/sys/class/gpio/unexport", GPIO_PIN);
return 0; // 退出程序 / Exit the program
}
Comments
0 comments
Article is closed for comments.