pile_nav_new/lib/pages/dataAndDevice/controller.dart

151 lines
4.2 KiB
Dart
Raw Permalink Normal View History

2024-11-20 19:42:10 +08:00
import 'dart:async';
import 'dart:core';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:roslibdart/roslibdart.dart';
import '../../service/base.dart';
class PlumRealDataController extends GetxController {
var isGenerate = false.obs; //是否生成
var angle = 0.0.obs; //方向弧度
var isDirect = false.obs; //是否为方向设置
var checkValue = "".obs; //checkPile -桩点坐标,checkDirection-方向设置
var checkName = "".obs;
var centerXY = Offset.zero.obs; //中心点
var space = 5.0.obs; //间距 m
var genLenth = 0.obs; //半径
var isPileId = false.obs;
var direction = 0.0.obs; //角度
var centerOffset = Offset.zero.obs;
var linePoint = Offset.zero.obs;
var isUp = false.obs;
var plumList = [].obs;
var shouldPaint = true.obs;
late Service service;
late Ros ros;
@override
void onInit() async {
super.onInit();
ros = BaseService().getRos();
// ros.connect();
// service = Service(
// name: '/smash_point_list_service',
// ros: ros,
// type: "nav2_smash_behavior/srv/SmashPointList");
// var json = {'data': []};
// service.call(json).then((value) {
// print('ros$value');
// });
}
updateLinePoint(Offset offset) {
linePoint.value = offset;
update();
}
// 监听 plumList 变量的变化
void listenToPlumList() {
plumList.listen((list) {
var points = [];
for (var i = 0; i < list.length; i++) {
Offset item = list[i];
points.add({"x": item.dx, "y": item.dy, "z": 0});
}
service
.call({"points": points})
.then((value) => {print("----$value")})
.catchError((e) => {print(e)});
}, onError: (err) {
// 错误处理
});
}
}
class RealDataController extends GetxController {
final time = DateTime.now().obs;
final tid = 100.obs;
final name = '桩点名称'.obs;
final sid = '10.222'.obs;
final times = 10.obs;
2024-11-21 10:10:51 +08:00
final isClicked = false.obs;
2024-11-20 19:42:10 +08:00
late final lineDis = 110.0.obs;
late final distance = 460.0.obs;
var isHooked = false.obs;
late Topic topic;
late Ros ros;
double g = 9.8; // 重力加速度
double t = 0; // 时间
@override
void onInit() async {
super.onInit();
ros = BaseService().getRos();
// ros.connect();
// topic = Topic(
// ros: ros,
// name: '/your_topic',
// type: 'std_msgs/String',
// );
// // 订阅者来接收 ROS 主题的消息
// topic.subscribe((message) async {
// print('Received message: $message');
// });
// ActionClient action = ActionClient(
// ros: ros,
// actionName: '/virtual_odom',
// serverName: 'virtual_odom_publisher_behavior/action/PublishVirtualOdom',
// goalName: "/target_pose",
// goalType: 'geometry_msgs/PoseStamped');
// await action.init();
// action.feedbacker.subscription!.listen((message) {
// String g = message['status']['goal_id']['id'];
// action.goals[g]!.stream.listen((data) {
// // 在这里处理 status 的变化
// print('Status changed: ${data['status']}');
// });
// });
// Map message = {
// 'target_pose': {
// 'header': {'frame_id': 'map'},
// 'pose': {
// 'position': {'x': 10.0, 'y': 10.0, 'z': 0.0},
// 'orientation': {'x': 0.0, 'y': 0.0, 'z': 0.0, 'w': 1.0}
// }
// }
// };
// action.send(message);
// 定时器每秒更新时间
Timer.periodic(const Duration(milliseconds: 100), (_) {
time.value = DateTime.now();
if (!isHooked.value && lineDis < 560 && distance >= 460) {
lineDis.value += 10; // 每秒增加10
} else if (lineDis > 110 && distance <= 460) {
isHooked.value = true;
distance.value -= 10;
lineDis.value -= 10;
} else if (distance.value < 460) {
isHooked.value = false;
t += 1; // 每秒增加1秒
distance.value = 0.5 * g * t * t;
if (distance.value >= 460) {
distance.value = 460;
t = 0;
}
lineDis.value = 110;
}
});
}
@override
void dispose() async {
super.dispose();
await ros.close();
}
}