我的Schedule


36390 I&C SCI   142 LEC 01:00-01:50   CS   174
36391 I&C SCI   142 DIS 02:00-02:50   CS   174
36393 I&C SCI   142 LAB 03:00-03:50   CS   183

36450 I&C SCI   161 LEC 09:30-10:50   RH   101
32000 PHILOS      1 LEC 11:00-12:20   HSLH 100A
18024 ASIANAM   60A DIS 01:00-01:50   HICF 100Q
18020 ASIANAM   60A LEC 03:30-04:50   SSH  100

36451 I&C SCI   161 DIS 09:00-09:50   HICF 100N
36390 I&C SCI   142 LEC 01:00-01:50   CS   174
36350 I&C SCI   131 LEC 04:00-05:20   ELH  100

36450 I&C SCI   161 LEC 09:30-10:50   RH   101
32000 PHILOS      1 LEC 11:00-12:20   HSLH 100A
18020 ASIANAM   60A LEC 03:30-04:50   SSH  100
36353 I&C SCI   131 DIS 05:00-05:50   SST  220A

36451 I&C SCI   161 DIS 09:00-09:50   HICF 100N
32006 PHILOS      1 DIS 12:00-12:50   HOB2 129
36390 I&C SCI   142 LEC 01:00-01:50   CS   174
36350 I&C SCI   131 LEC 04:00-05:20   ELH  100

为了根据抬升到位停3s、下降到位停3s的需求调用给定的 `schedule` 函数,需要对 `Phase` 结构体和 `schedule` 函数进行适当的修改和扩展。以下是实现该功能的函数框架: ```cpp #include <iostream> #include <vector> #include <chrono> #include <thread> // 假设 GetCurrentTickMs 函数的实现 uint64_t GetCurrentTickMs() { auto now = std::chrono::steady_clock::now(); return std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count(); } // 定义 Phase 结构体 struct Phase { void (*action)(); // 动作函数指针 uint64_t relativeTime; // 阶段持续时间 }; // 抬升动作函数 void liftAction() { std::cout << "抬升动作执行" << std::endl; } // 下降动作函数 void lowerAction() { std::cout << "下降动作执行" << std::endl; } // 等待3秒动作函数 void waitThreeSeconds() { std::this_thread::sleep_for(std::chrono::seconds(3)); std::cout << "等待3秒完成" << std::endl; } // 原有的 schedule 函数 void schedule(const std::vector<Phase>& phases, int loop_count) { static int currentPhase = 0; static uint64_t currentPhaseStartTickMs = 0; static int phaseSize = phases.size(); static bool first_in = true; if(0 == phaseSize || 0 == loop_count) { // TODO: 参数错误,直接退出,设置标志位 return; } // 首次执行,启动 if(true == first_in) { phases[currentPhase].action(); currentPhaseStartTickMs = GetCurrentTickMs(); first_in = false; } // 当前时间减去当前阶段开始时间(ms),如果大于当前阶段的时间,则进入下一个阶段 if(GetCurrentTickMs() - currentPhaseStartTickMs >= phases[currentPhase % phaseSize].relativeTime) { currentPhase += 1; if(currentPhase == loop_count * phaseSize) { // TODO: 执行完成,退出,执行退出动作 return; } // 执行下一个阶段 phases[currentPhase % phaseSize].action(); currentPhaseStartTickMs = GetCurrentTickMs(); } } // 调用 schedule 函数实现抬升到位停3s,下降到位停3s的函数 void executeLiftAndLower() { std::vector<Phase> phases = { {liftAction, 0}, {waitThreeSeconds, 3000}, {lowerAction, 0}, {waitThreeSeconds, 3000} }; int loop_count = 1; schedule(phases, loop_count); } int main() { executeLiftAndLower(); return 0; } ``` ### 代码解释 - **`Phase` 结构体**:包含一个动作函数指针 `action` 和阶段持续时间 `relativeTime` [^1]。 - **`liftAction` 和 `lowerAction` 函数**:分别表示抬升和下降动作。 - **`waitThreeSeconds` 函数**:用于等待3秒。 - **`schedule` 函数**:原有的调度函数,根据阶段持续时间调度不同的动作。 - **`executeLiftAndLower` 函数**:调用 `schedule` 函数实现抬升到位停3s,下降到位停3s的功能。
评论 5
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值