Skip to content

Commit

Permalink
Merge branch 'CR_8760_compiling_ethercat_into_sdcard_image_atlas.luo'…
Browse files Browse the repository at this point in the history
… into 'jh7110-devel'

CR_8760_compiling_ethercat_into_sdcard_image_atlas.luo

See merge request sdk/soft_3rdpart!81
  • Loading branch information
andyhu-stf committed Dec 21, 2023
2 parents 7ec8b82 + 9b92777 commit 819f2f5
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 46 deletions.
8 changes: 8 additions & 0 deletions igh_ethercat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ README.md 本文件
./igh_ethercat.sh
```

或添加`img`参数同时编译sdcard.img:

```bash
./igh_ethercat.sh img
```

#### 说明

该脚本会检测SDK所使用的 linux 内核版本是否为 rt-ethercat-release 且是否已经编译过,若确认在该分支上且编译过,则会拉取指定版本的 IgH EtherCAT 主站代码,进行主站以及Demo程序的编译。
Expand All @@ -28,6 +34,8 @@ README.md 本文件

IgH EtherCAT主站启动脚本以及Demo程序会在主站编译完成后编译并安装到 work/buildroot_initramfs_sysroot/root 路径,即打包后的镜像的 /root 路径下。

添加`img`参数同时编译sdcard.img时,脚本会同时检测 work/buildroot_rootfs 是否存在,否则会提示先执行`make buildroot_rootfs -j$(nproc)`。成功完毕脚本后,IgH EtherCAT主站启动脚本以及Demo程序会在主站编译完成后编译并安装到 work/buildroot_rootfs/target/root 路径下,并打包至sdcard.img中。

### 启动脚本

EtherCAT 主站的使用需要绑定对应网口的 MAC 地址号,此 MAC 地址需要在 U-boot 命令行模式下通过命令获取:
Expand Down
112 changes: 68 additions & 44 deletions igh_ethercat/application/main_profile_velocity_2.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#define DIFF_NS(A, B) (((B).tv_sec - (A).tv_sec) * NSEC_PER_SEC + (B).tv_nsec - (A).tv_nsec)
#define TIMESPEC2NS(T) ((uint64_t)(T).tv_sec * NSEC_PER_SEC + (T).tv_nsec)


#ifdef MEASURE_TIMING
// Log tht save data.
char logname[50];
Expand Down Expand Up @@ -210,6 +209,11 @@ void check_master_state()
if (ms.link_up != master_state.link_up)
{
printf("Link is %s.\n", ms.link_up ? "up" : "down");

if (!ms.link_up)
{
is_quit = true;
}
}

master_state = ms;
Expand Down Expand Up @@ -238,6 +242,11 @@ void check_slave_config_state(struct _SlaveConfig *slave_config)
printf("slave[%d]: %s.\n", i, s.online ? "online" : "offline");
}

if (!s.online)
{
is_quit = true;
}

if (s.operational != slave_config[i].sc_state.operational)
{
// The slave was brought into \a OP state using the specified configuration.
Expand Down Expand Up @@ -273,6 +282,7 @@ void cyclic_task(struct _SlaveConfig *slave_config, struct _Domain *domain)
// Used to determine the value of the status word.
uint16_t proof_value = 0x004F;
uint16_t ctrl_word[] = {0x0006, 0x0007, 0x000f};
uint16_t fault_reset = 0x008f;
uint16_t status;
uint32_t count = 0;
static int direction = 0;
Expand Down Expand Up @@ -366,28 +376,33 @@ void cyclic_task(struct _SlaveConfig *slave_config, struct _Domain *domain)
{
// Read state word.
status = EC_READ_U16(domain->domain_pd + slave_offset[i].Status_Word);
if ((status & proof_value) == 0x0040)
switch (status & proof_value)
{
// Control phase 1.
case 0x0040:
// Set operation mode to velocity Mode.
EC_WRITE_U8(domain->domain_pd + slave_offset[i].Operation_Mode, MOTOR_MODEL_PROFILE_VELOCITY);
// Set Profile_Acceleration and Profile_Deceleration.
// Set Profile_Acceleration and Profile_Deceleration.
EC_WRITE_U32(domain->domain_pd + slave_offset[i].Profile_Acceleration, MOTOR_PROFILE_ACCELERATION);
EC_WRITE_U32(domain->domain_pd + slave_offset[i].Profile_Deceleration, MOTOR_PROFILE_DECELERATION);
EC_WRITE_U16(domain->domain_pd + slave_offset[i].Ctrl_Word, ctrl_word[0]);
proof_value = 0x006F;
}
else if ((status & proof_value) == 0x0021)
{
break;

// Control phase 2.
case 0x0021:
EC_WRITE_U16(domain->domain_pd + slave_offset[i].Ctrl_Word, ctrl_word[1]);
proof_value = 0x006F;
}
else if ((status & proof_value) == 0x0023)
{
break;

// Control phase 3.
case 0x0023:
EC_WRITE_U16(domain->domain_pd + slave_offset[i].Ctrl_Word, ctrl_word[2]);
proof_value = 0x006F;
}
else if ((status & proof_value) == 0x0027)
{
break;

// Control phase 3.
case 0x0027:
slave_config[i].current_velocity = EC_READ_U32(domain->domain_pd + slave_offset[i].Current_Velocity);
// Determine whether to turn forward or reverse.
if (direction == 0)
Expand Down Expand Up @@ -416,11 +431,12 @@ void cyclic_task(struct _SlaveConfig *slave_config, struct _Domain *domain)
direction = 0;
}
}
}
else
{
break;

default:
// Fault reset.
EC_WRITE_U16(domain->domain_pd + slave_offset[i].Ctrl_Word, 0x008f);
break;
}
}

Expand All @@ -432,6 +448,8 @@ void cyclic_task(struct _SlaveConfig *slave_config, struct _Domain *domain)
clock_gettime(CLOCK_TO_USE, &endTime);
#endif
}

printf("Exit cyclic task.\n");
}

// Function stop servo.
Expand Down Expand Up @@ -466,16 +484,6 @@ static void sig_handle(int signal)
printf("Manually exit the program!\n");
}

// Function error leaving program.
void err_leave(void)
{
// Releases EtherCAT master.
ecrt_release_master(master);
printf("Release master.\n");
fclose(logfile);
exit(EXIT_FAILURE);
}

// Function initial the ethercat.
int init_ethercat(struct _SlaveConfig *slave_config, int *ret, int *status)
{
Expand All @@ -498,7 +506,7 @@ int init_ethercat(struct _SlaveConfig *slave_config, int *ret, int *status)
{
*status = -1;
printf("Create domain failed.\n");
err_leave();
goto err_leave;
}
printf("Create domain success.\n");

Expand All @@ -512,7 +520,7 @@ int init_ethercat(struct _SlaveConfig *slave_config, int *ret, int *status)
{
*status = -1;
printf("Get slave configuration failed.\n");
err_leave();
goto err_leave;
}
}
printf("Get slave configuration success.\n");
Expand All @@ -525,7 +533,7 @@ int init_ethercat(struct _SlaveConfig *slave_config, int *ret, int *status)
{
*status = -1;
printf("Configuration PDO failed.\n");
err_leave();
goto err_leave;
}
}
printf("Configuration PDO success.\n");
Expand All @@ -536,7 +544,7 @@ int init_ethercat(struct _SlaveConfig *slave_config, int *ret, int *status)
{
*status = -1;
printf("Failed to register bunch of PDO entries for domain.\n");
err_leave();
goto err_leave;
}
printf("Success to register bunch of PDO entries for domain.\n");

Expand All @@ -546,7 +554,7 @@ int init_ethercat(struct _SlaveConfig *slave_config, int *ret, int *status)
{
*status = -1;
printf("Activate master failed.\n");
err_leave();
goto err_leave;
}
printf("Activate master success.\n");

Expand All @@ -556,11 +564,21 @@ int init_ethercat(struct _SlaveConfig *slave_config, int *ret, int *status)
{
*status = -1;
printf("Get pointer to the process data memory failed.\n");
err_leave();
goto err_leave;
}
printf("Get pointer to the process data memory success.\n");

return 0;
return *status;

// Function error leaving program.
err_leave:
// Releases EtherCAT master.
ecrt_release_master(master);
printf("Release master.\n");
#ifdef MEASURE_TIMING
fclose(logfile);
#endif
return *status;
}

int main(int argc, char **argv)
Expand All @@ -580,6 +598,7 @@ int main(int argc, char **argv)

memset(&slave_config, 0, sizeof(slave_config));

#ifdef MEASURE_TIMING
// Create a .csv file named by time.
time_t current_time;
struct tm *time_info;
Expand All @@ -590,7 +609,7 @@ int main(int argc, char **argv)

snprintf(logname, sizeof(logname), "%s.csv", time_buffer);

logfile= fopen(logname, "w");
logfile = fopen(logname, "w");

if (logfile == NULL)
{
Expand All @@ -600,32 +619,37 @@ int main(int argc, char **argv)

// Defining List headers.
fwrite("ID, period_min,period_max,exec_min,exec_max,latency_min,latency_max,\r\n", 1, strlen("ID, period_min,period_max,exec_min,exec_max,latency_min,latency_max,\r\n"), logfile);
#endif

// init ethercat
init_ethercat(slave_config, &ret, &status);
if (init_ethercat(slave_config, &ret, &status) == -1)
{
perror("init ethercat fail.");
exit(EXIT_FAILURE);
}

// Set highest priority.
pid_t pid = getpid();

struct sched_param param;

int max_priority = sched_get_priority_max(SCHED_FIFO);

int min_priority = sched_get_priority_min(SCHED_FIFO);

param.sched_priority = max_priority;

if (sched_setscheduler(pid, SCHED_FIFO, &param) == -1) {
perror("sched_setscheduler");

if (sched_setscheduler(pid, SCHED_FIFO, &param) == -1)
{
perror("sched_setscheduler");
exit(EXIT_FAILURE);
}

printf("Max priority: %d\n", max_priority);

printf("Min priority: %d\n", min_priority);

printf("Current priority: %d\n", sched_get_priority_max(SCHED_FIFO));

printf("Current priority: %d\n", sched_get_priority_max(SCHED_FIFO));

// Start cyclic function.
printf("Run cycle task...\n");
Expand Down
37 changes: 35 additions & 2 deletions igh_ethercat/igh_ethercat.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,30 @@
current_path=$(pwd)
work_path=${current_path}/../../work
buildroot_initramfs_sysroot_path=${work_path}/buildroot_initramfs_sysroot
buildroot_rootfs_path=${work_path}/buildroot_rootfs
linux_path=${work_path}/linux
kernel_release_file=${linux_path}/include/config/kernel.release
install_mod_path=${work_path}/module_install_path
toolchains_path=${work_path}/buildroot_initramfs/host/bin

sdcard_img=0

# Determine if compile 'sdcard.img' and check if the root filesystem is compiled.
if [ "$#" -eq 0 ]; then
echo "Compile 'image.fit' only."
echo "If you need to compile 'sdcard.img', usage: '$0 img'"
elif [ "$1" = "img" ]; then
if [ -d "${buildroot_rootfs_path}" ]; then
echo "Compile both 'image.fit' and 'sdcard.img'"
sdcard_img=1
else
echo "Could not add application to sdcard image, please run 'make buildroot_rootfs -j$(nproc)' first."
exit 1
fi
else
echo "The argument is not 'img'"
fi

cd ../../linux
linux_branch=$(git rev-parse --abbrev-ref HEAD)

Expand All @@ -16,7 +35,7 @@ if [ "$linux_branch" == "rt-ethercat-release" ]; then
git pull
cd ../
make clean
make
make -j$(nproc)
else
echo "The Linux source code is not on the 'rt-ethercat-release' branch. Exiting."
cd ${current_path}
Expand Down Expand Up @@ -148,6 +167,11 @@ CC=${CC} make

cp ectest_PV ${buildroot_initramfs_sysroot_path}/root

if [ $sdcard_img -eq 1 ]; then
echo "Copy application to '${buildroot_rootfs_path}/target/root'."
cp ectest_PV ${buildroot_rootfs_path}/target/root
fi

echo ""
echo "==============================Generating 'start_ethercat_master.sh'=============================="

Expand Down Expand Up @@ -178,11 +202,20 @@ EOF

chmod +x start_ethercat_master.sh

if [ $sdcard_img -eq 1 ]; then
echo "Copy script to '${buildroot_rootfs_path}/target/root'."
cp start_ethercat_master.sh ${buildroot_rootfs_path}/target/root
fi

echo ""
echo "==============================Re-compiling SDK=============================="

cd ${current_path}/../../

make
make -j$(nproc)

if [ $sdcard_img -eq 1 ]; then
make img
fi

cd ${current_path}

0 comments on commit 819f2f5

Please sign in to comment.