Rmaxis.cs 15.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MotorMaster.Sdk;

namespace Neotel
{
    /// <summary>
    /// 增广智能电动夹爪(RM-C控制器)
    /// </summary>
    public class Rmaxis
    {
        private RMAxis device;
        private readonly log4net.ILog LOG;
        private readonly bool initOK;
        public readonly string[] ErrorString = new string[] { "正常", "通用错误", "电机失相", "位置超差", "速度超差", "电机堵转", "初相励磁错误" };
        
        /// <summary>
        /// 增广智能电动夹爪(RM-C控制器)
        /// </summary>
        /// <param name="logName">日志名</param>
        public Rmaxis(string logName = "Rmaxis")
        {
            LOG = log4net.LogManager.GetLogger(logName);

            try
            {
                //使用SDK前,动态加载DLL和Config
                RMAxis.Initialize(Environment.CurrentDirectory + "\\SDK\\MotorMaster.Sdk.dll");
                RMAxis.SetConfigPath(Environment.CurrentDirectory + "\\SDK\\MotorMaster.config");
                LOG.Info("Rmaxis Init OK");
                initOK = true;
            }
            catch (Exception ex)
            {
                LOG.Error("Rmaxis Init", ex);
                initOK = false;
            }
        }

        /// <summary>
        /// 串口是否连接
        /// </summary>
        public bool IsPortOpen { private set; get; } = false;

        ///// <summary>
        ///// 伺服是否打开
        ///// </summary>
        //public bool IsServoOn
        //{
        //    get => device.ServoOnOff;
        //}

        /// <summary>
        /// 轴是否在运动
        /// </summary>
        public bool IsMoving { get => device.IsMoving(); }

        /// <summary>
        /// 轴是否到达运动目标
        /// </summary>
        public bool IsReached { get => device.IsReached(); }

        /// <summary>
        /// 轴是否推空(可以在Push完成后检测是否推压/夹持为空)
        /// </summary>
        public bool IsPushEmpty { get => device.IsPushEmpty(); }

        /// <summary>
        /// 错误代码
        /// </summary>
        public int ErrorCode { get => (int)device.ErrorCode; }

        //========================================
        /// <summary>
        /// 打开串口
        /// </summary>
        /// <param name="portName">串口名</param>
        /// <param name="axisNo">轴号</param>
        /// <returns></returns>
        public bool OpenPort(string portName, ushort axisNo = 0)
        {
            if (initOK)
            {
                try
                {
                    device = RMAxis.CreateModbus("\\\\.\\" + portName.ToUpper(), 115200, axisNo);
                    LOG.Info("OpenPort OK");
                    IsPortOpen = true;
                }
                catch (Exception ex)
                {
                    LOG.Error("OpenPort", ex);
                    IsPortOpen = false;
                }
            }
            else
            {
                IsPortOpen = false;
            }
            return IsPortOpen;
        }

        /// <summary>
        /// 关闭串口
        /// </summary>
        public void ClosePort()
        {
            if (!initOK) return;
            if (device == null) return;
            IsPortOpen = false;

            try
            {
                RMAxis.Destroy(device);
                device = null;
                LOG.Info("ClosePort OK");
            }
            catch (Exception ex)
            {
                LOG.Error("ClosePort", ex);
            }
        }

        /// <summary>
        /// 重置控制器错误
        /// </summary>
        public void ResetError()
        {
            if (!IsPortOpen) return;
            try
            {
                device.ResetError();
                LOG.Info("ResetError OK");
            }
            catch (Exception ex)
            {
                LOG.Error("ResetError", ex);
            }
        }

        ///// <summary>
        ///// 打开伺服
        ///// </summary>
        //public void ServoOn()
        //{
        //    if (!IsPortOpen) return;
        //    try
        //    {
        //        if (!device.ServoOnOff)
        //            device.ServoOnOff = true;
        //        LOG.Info("ServoOn OK");
        //    }
        //    catch (Exception ex)
        //    {
        //        LOG.Error("ServoOn", ex);
        //    }
        //}

        ///// <summary>
        ///// 关闭伺服
        ///// </summary>
        //public void ServoOff()
        //{
        //    if (!IsPortOpen) return;
        //    try
        //    {
        //        if (device.ServoOnOff)
        //            device.ServoOnOff = false;
        //        LOG.Info("ServoOff OK");
        //    }
        //    catch (Exception ex)
        //    {
        //        LOG.Error("ServoOff", ex);
        //    }
        //}

        /// <summary>
        /// 停止轴运动
        /// </summary>
        public void StopAxis()
        {
            if (!IsPortOpen) return;
            try
            {
                device.Stop();
                LOG.Info("StopAxis OK");
            }
            catch (Exception ex)
            {
                LOG.Error("StopAxis", ex);
            }
        }

        //========================================
        /// <summary>
        /// 控制器版本号
        /// </summary>
        /// <returns></returns>
        public string GetVersion()
        {
            RMAxis.Native.version_t version = device.Version;
            return string.Format("{0}.{1}.{2}.{3}", version.major, version.minor, version.build, version.type);
        }

        /// <summary>
        /// 获取当前设置的出力值
        /// </summary>
        /// <returns>出力,%</returns>
        public float GetTorque()
        {
            float n = -1;
            if (!IsPortOpen) return n;
            try
            {
                n = device.Torque;
                LOG.Info("GetTorque OK");
            }
            catch (Exception ex)
            {
                n = -1;
                LOG.Error("GetTorque", ex);
            }
            return n;
        }

        ///// <summary>
        ///// 获取电机运动中的出力
        ///// </summary>
        ///// <returns>出力,%</returns>
        //public float GetForce()
        //{
        //    float n = -1;
        //    if (!IsPortOpen) return n;
        //    try
        //    {
        //        n = device.ForceSensor;
        //        LOG.Info("GetForce OK");
        //    }
        //    catch (Exception ex)
        //    {
        //        n = -1;
        //        LOG.Error("GetForce", ex);
        //    }
        //    return n;
        //}

        /// <summary>
        /// 获取当前电机速度
        /// </summary>
        /// <returns>速度,mm/s</returns>
        public float GetVelocity()
        {
            float n = -1;
            if (!IsPortOpen) return n;
            try
            {
                n = device.Velocity;
                LOG.Info("GetVelocity OK");
            }
            catch (Exception ex)
            {
                n = -1;
                LOG.Error("GetVelocity", ex);
            }
            return n;
        }

        /// <summary>
        /// 获取当前位置
        /// </summary>
        /// <returns>位置,mm</returns>
        public float GetPosition()
        {
            float n = -1;
            if (!IsPortOpen) return n;
            try
            {
                n = device.Position;
                LOG.Info("GetPosition OK");
            }
            catch (Exception ex)
            {
                n = -1;
                LOG.Error("GetPosition", ex);
            }
            return n;
        }

        //========================================
        /// <summary>
        /// 回原点
        /// </summary>
        public void GoHome()
        {
            if (!IsPortOpen) return;
            LOG.Info("GoHome Start");

            try
            {
                device.GoHome();
                LOG.Info("GoHome OK");
            }
            catch (Exception ex)
            {
                LOG.Error("GoHome", ex);
            }
        }

        /// <summary>
        /// 推压运动
        /// </summary>
        /// <param name="force">出力,%</param>
        /// <param name="distance">距离,mm</param>
        /// <param name="velocity">速度,mm/s</param>
        public void Push(float force, float distance, float velocity)
        {
            if (!IsPortOpen) return;
            LOG.Info("Push Start");

            try
            {
                device.Push(force, distance, velocity);
                LOG.Info("Push OK");
            }
            catch (Exception ex)
            {
                LOG.Error("Push", ex);
            }
        }

        /// <summary>
        /// 绝对运动
        /// </summary>
        /// <param name="position">位置,mm</param>
        /// <param name="velocity">速度,mm/s</param>
        /// <param name="acceleration">加速度,mm/s^2</param>
        /// <param name="deacceleration">减速度,mm/s^2</param>
        /// <param name="band">定位范围,mm</param>
        public void MoveAbsolute(float position, float velocity, float acceleration, float deacceleration, float band)
        {
            if (!IsPortOpen) return;
            LOG.Info("MoveAbsolute Start");

            try
            {
                device.MoveAbsolute(position, velocity, acceleration, deacceleration, band);
                LOG.Info("MoveAbsolute OK");
            }
            catch (Exception ex)
            {
                LOG.Error("MoveAbsolute", ex);
            }
        }

        /// <summary>
        /// 直接运动到指定位置
        /// </summary>
        /// <param name="position">位置,mm</param>
        public void MoveTo(float position)
        {
            if (!IsPortOpen) return;
            LOG.Info("MoveTo Start");

            try
            {
                device.MoveTo(position);
                LOG.Info("MoveTo OK");
            }
            catch (Exception ex)
            {
                LOG.Error("MoveTo", ex);
            }
        }

        /// <summary>
        /// 直接运动的参数
        /// </summary>
        /// <param name="velocity">速度,mm/s</param>
        /// <param name="acceleration">加速度,mm/s^2</param>
        /// <param name="deacceleration">减速度,mm/s^2</param>
        public void MoveConfig(float velocity, float acceleration, float deacceleration)
        {
            if (!IsPortOpen) return;
            try
            {
                device.ConfigMotion(velocity, acceleration, deacceleration);
                LOG.Info("MoveConfig OK");
            }
            catch (Exception ex)
            {
                LOG.Error("MoveConfig", ex);
            }
        }

        /// <summary>
        /// 回原点,并等到到位
        /// </summary>
        /// <param name="timeout">超时时间,ms</param>
        /// <returns></returns>
        public bool GoHomeWait(int timeout)
        {
            if (!IsPortOpen) return false;
            LOG.Info("GoHomeWait Start");
            bool result;

            try
            {
                device.GoHome();
                //device.WaitComplete((uint)timeout);
                //result = device.IsReached();
                result = Wait(timeout);
                if (result)
                    LOG.Info("GoHomeWait OK");
                else
                    LOG.Info("GoHomeWait Timeout");
            }
            catch (Exception ex)
            {
                result = false;
                LOG.Error("GoHomeWait", ex);
            }
            return result;
        }

        /// <summary>
        /// 推压运动,并等到到位
        /// </summary>
        /// <param name="force">出力,%</param>
        /// <param name="distance">距离,mm</param>
        /// <param name="velocity">速度,mm/s</param>
        /// <param name="timeout">超时时间,ms</param>
        /// <returns></returns>
        public bool PushWait(float force, float distance, float velocity, int timeout)
        {
            if (!IsPortOpen) return false;
            LOG.Info("PushWait Start");
            bool result;

            try
            {
                device.Push(force, distance, velocity);
                //device.WaitComplete((uint)timeout);
                //result = device.IsReached();
                result = Wait(timeout);
                if (result)
                    LOG.Info("PushWait OK");
                else
                    LOG.Info("PushWait Timeout");
            }
            catch (Exception ex)
            {
                result = false;
                LOG.Error("PushWait", ex);
            }
            return result;
        }

        /// <summary>
        /// 绝对运动,并等到到位
        /// </summary>
        /// <param name="position">位置,mm</param>
        /// <param name="velocity">速度,mm/s</param>
        /// <param name="acceleration">加速度,mm/s^2</param>
        /// <param name="deacceleration">减速度,mm/s^2</param>
        /// <param name="band">定位范围,mm</param>
        /// <param name="timeout">超时时间,ms</param>
        /// <returns></returns>
        public bool MoveAbsoluteWait(float position, float velocity, float acceleration, float deacceleration, float band, int timeout)
        {
            if (!IsPortOpen) return false;
            LOG.Info("MoveAbsoluteWait Start");
            bool result;

            try
            {
                device.MoveAbsolute(position, velocity, acceleration, deacceleration, band);
                //device.WaitComplete((uint)timeout);
                //result = device.IsReached();
                result = Wait(timeout);
                if (result)
                    LOG.Info("PushWait OK");
                else
                    LOG.Info("PushWait Timeout");
            }
            catch (Exception ex)
            {
                result = false;
                LOG.Error("MoveAbsoluteWait", ex);
            }
            return result;
        }

        /// <summary>
        /// 直接运动到指定位置,并等到到位
        /// </summary>
        /// <param name="position">位置,mm</param>
        /// <param name="timeout">超时时间,ms</param>
        /// <returns></returns>
        public bool MoveToWait(float position, int timeout)
        {
            if (!IsPortOpen) return false;
            LOG.Info("MoveToWait Start");
            bool result;

            try
            {
                device.MoveTo(position);
                //device.WaitComplete((uint)timeout);
                //result = device.IsReached();
                result = Wait(timeout);
                if (result)
                    LOG.Info("PushWait OK");
                else
                    LOG.Info("PushWait Timeout");
            }
            catch (Exception ex)
            {
                result = false;
                LOG.Error("MoveToWait", ex);
            }
            return result;
        }



        private bool Wait(int timeout)
        {
            int n = 0;
            int mm = 10;
            bool result = true;
            System.Threading.Thread.Sleep(mm);

            while (!device.IsReached())
            {
                if (n >= timeout)
                {
                    result = false;
                    break;
                }
                n += mm;
                System.Threading.Thread.Sleep(mm);
                System.Windows.Forms.Application.DoEvents();
            }
            return result;
        }



    }
}