Description of Project 1.
Language: c++/arduino/html/css/js
Stars: free
#include
#include
class Leg {
public:
Leg(double _L1, double _L2) : L1(_L1), L2(_L2) {}
void setJoint(double _joint1, double _joint2) {
joint1 = _joint1;
joint2 = _joint2;
}
std::vector getIK(double x, double y, double z) {
std::vector angles;
double xc = sqrt(x * x + y * y);
double deltaL = sqrt(xc * xc + (z - L1) * (z - L1));
if (deltaL > L1 + L2 || deltaL < abs(L1 - L2)) {
return angles;
}
double cosTheta2 = (deltaL * deltaL - L1 * L1 - L2 * L2) / (2 * L1 * L2);
double sinTheta2 = sqrt(1 - cosTheta2 * cosTheta2);
double theta2 = atan2(sinTheta2, cosTheta2);
double sinTheta1 = ((L1 + L2 * cosTheta2) * z - L2 * sinTheta2 * xc) / (xc * xc + (z - L1) * (z - L1));
double cosTheta1 = (xc - L2 * sinTheta2 * z) / (xc * xc + (z - L1) * (z - L1));
double theta1 = atan2(sinTheta1, cosTheta1);
angles.push_back(theta1);
angles.push_back(theta2);
return angles;
}
private:
double L1;
double L2;
double joint1;
double joint2;
};
class Robot {
public:
Robot() : legs{Leg(80, 200), Leg(80, 200), Leg(80, 200), Leg(80, 200)} {}
void setFootPos(int legIndex, double x, double y, double z) {
auto angles = legs[legIndex].getIK(x, y, z);
if (angles.size() == 2) {
legs[legIndex].setJoint(angles[0], angles[1]);
}
else {
std::cout << "Invalid Foot Position!" << std::endl;
}
}
void printJointAngles() {
for (auto leg : legs) {
std::cout << "Joint1: " << leg.joint1 << ", Joint2: " << leg.joint2 << std::endl;
}
}
private:
std::vector legs;
};
int main() {
Robot robot;
robot.setFootPos(0, 180, 50, -100);
robot.setFootPos(1, 180, -50, -100);
robot.setFootPos(2, 80, -50, -100);
robot.setFootPos(3, 80, 50, -100);
robot.printJointAngles();
return 0;
}
View Details
Description of Project 2.
Language: js
Stars: free
// 计算Levenshtein距离
function levenshteinDistance(a, b) {
if (a.length === 0) return b.length;
if (b.length === 0) return a.length;
const matrix = [];
// 初始化矩阵
for (let i = 0; i <= b.length; i++) {
matrix[i] = [i];
}
for (let j = 0; j <= a.length; j++) {
matrix[0][j] = j;
}
// 计算编辑距离
for (let i = 1; i <= b.length; i++) {
for (let j = 1; j <= a.length; j++) {
if (b.charAt(i - 1) === a.charAt(j - 1)) {
matrix[i][j] = matrix[i - 1][j - 1];
} else {
matrix[i][j] = Math.min(
matrix[i - 1][j - 1] + 1, // 替换
matrix[i][j - 1] + 1, // 插入
matrix[i - 1][j] + 1 // 删除
);
}
}
}
return matrix[b.length][a.length];
}
// Store the user's history of questions and responses
let previousQuestion = '';
let previousResponse = '';
let repeatedQuestionCount = 0; // 记录用户反复询问相同问题的次数
let impatienceLevel = 0; // 不耐烦程度
// 发送消息函数
async function sendMessage() {
const userInput = document.getElementById('user-input').value.trim(); // 去除用户输入的空格
// 如果用户输入包含 "write code"
if (userInput.toLowerCase().includes('write code')) {
const codeToExecute = userInput.substring(userInput.indexOf('write code') + 10).trim(); // 提取 "write code" 后面的内容
try {
// 尝试执行用户输入的代码
const codeResult = eval(codeToExecute); // 使用 eval 函数执行代码
if (typeof codeResult !== 'undefined') {
displayMessage('bot', `Code execution result: ${codeResult}`); // 显示代码执行结果
} else {
displayMessage('bot', `Code executed successfully, but no result returned.`);
}
} catch (error) {
// 如果执行代码时出错,显示错误消息
displayMessage('bot', `Error executing code: ${error.message}`);
}
document.getElementById('user-input').value = '';
return;
}
// 如果用户输入包含 "previous question" 并且之前有问题和回答
if (userInput.toLowerCase().includes('previous question') && previousQuestion && previousResponse) {
displayMessage('bot', previousQuestion);
displayMessage('bot', previousResponse);
return;
}
// 如果用户反复问同一个问题,机器人表现出不耐烦
if (userInput === previousQuestion) {
repeatedQuestionCount++;
if (repeatedQuestionCount >= 5) {
switch (impatienceLevel) {
case 0:
displayMessage('bot', `I've already answered that question multiple times. Please ask something else.`);
break;
case 1:
displayMessage('bot', `I've already answered that question. Please ask something new.`);
break;
case 2:
displayMessage('bot', `I've already answered that question multiple times. Can you try something different?`);
break;
default:
displayMessage('bot', `I've already answered that question many times! It's starting to get annoying.`);
break;
}
impatienceLevel++;
repeatedQuestionCount = 0; // 重置计数器
} else {
displayMessage('bot', `I've already answered that question.`);
}
document.getElementById('user-input').value = '';
return;
}
// 尝试从预定义的回复中获取响应
let botResponse = predefinedResponses[userInput];
// 如果没有直接匹配用户输入的响应,则查找最接近的匹配项
if (!botResponse) {
let minDistance = Infinity;
let closestMatch = '';
// 查找最接近的问题
for (const question in predefinedResponses) {
const distance = levenshteinDistance(userInput, question.toLowerCase());
if (distance < minDistance) {
minDistance = distance;
closestMatch = question;
}
}
botResponse = predefinedResponses[closestMatch];
// 如果仍然没有回复,尝试在互联网上搜索相关内容
if (!botResponse) {
const searchResult = await searchOnInternet(userInput);
if (searchResult) {
botResponse = `${searchResult.title}: ${searchResult.link}`;
} else {
botResponse = "I'm sorry, I couldn't find any relevant information.";
}
}
}
// 记录当前问题和回答
previousQuestion = userInput;
previousResponse = botResponse;
// 显示用户输入消息
displayMessage('user', userInput,);
// 延迟显示机器人回复
await delay(300); // 延迟 1 秒
displayMessage('bot', botResponse);
document.getElementById('user-input').value = ''; // 清空输入框
}
// 延迟函数
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function displayMessage(sender, message) {
const chatMessagesDiv = document.getElementById('chat-messages');
const messageContainer = document.createElement('div');
messageContainer.className = 'message-container';
const messageDiv = document.createElement('div');
messageDiv.className = sender === 'user' ? 'user-bubble' : 'bot-bubble';
// 创建一个图标元素
const icon = document.createElement('img');
icon.src = sender === 'user' ? 'user.png' : 'hhhh.png'; // 根据发送者设置不同的图标
icon.className = 'icon'; // 添加图标的类名,方便样式设置
messageDiv.appendChild(icon);
// 创建消息文本元素
const messageText = document.createElement('span');
messageText.textContent = message;
messageDiv.appendChild(messageText);
// 将消息容器添加到聊天窗口中
messageContainer.appendChild(messageDiv);
chatMessagesDiv.appendChild(messageContainer);
}
View Details