{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "9cf09892-2e9f-4e30-8d30-a98b5405a769",
   "metadata": {},
   "outputs": [],
   "source": [
    "#PATH=\"${PATH:+${PATH}:}~/opt/bin\"   # appending\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a131be81-c3bc-4158-bd60-c0fbbd95434b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b]0;⊚slic\u0007"
     ]
    }
   ],
   "source": [
    "from slic.core.adjustable import Adjustable, PVAdjustable, DummyAdjustable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "5d9dd927-be4c-445f-9d71-ca7a2e4707c6",
   "metadata": {},
   "outputs": [],
   "source": [
    "Theta = DummyAdjustable(ID='THETA', name='Theta', process_time=1)\n",
    "TwoTheta = DummyAdjustable(ID='TWOTHETA', name='TwoTheta', process_time=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "2b7f0ae3-928f-442d-8901-a46f8f3ed0ec",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Theta: 0.00, TwoTheta: 0.00\n",
      "Theta: 0.40, TwoTheta: 0.80\n",
      "Theta: 0.80, TwoTheta: 1.60\n",
      "Theta: 1.20, TwoTheta: 2.40\n",
      "Theta: 1.60, TwoTheta: 3.20\n",
      "Theta: 2.00, TwoTheta: 4.00\n",
      "Theta: 2.40, TwoTheta: 4.80\n",
      "Theta: 2.80, TwoTheta: 5.60\n",
      "Theta: 3.20, TwoTheta: 6.40\n",
      "Theta: 3.60, TwoTheta: 7.20\n",
      "DummyAdjustable \"Theta\" at 4\n",
      "DummyAdjustable \"TwoTheta\" at 8\n"
     ]
    }
   ],
   "source": [
    "Theta.mv(4)\n",
    "TwoTheta.mv(8)\n",
    "\n",
    "while any([Theta.is_moving(), TwoTheta.is_moving()]):\n",
    "    \n",
    "    print(f\"Theta: {Theta.get_current_value():.2f}, TwoTheta: {TwoTheta.get_current_value():.2f}\")\n",
    "    time.sleep(0.1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "567d2b53-b8ae-4969-9f12-bd6e23784cb1",
   "metadata": {},
   "outputs": [],
   "source": [
    "from slic.devices.general.motor import Motor\n",
    "\n",
    "newport = Motor(\"SARES30-MOBI1:MOT_5\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "1f127ceb-4c01-4499-863a-3d7f67a92205",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Popen: returncode: None args: 'caqtdm -macro \"P=SARES30-MOBI1:,M=MOT_5\" mot...>"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CAQTDM_DISPLAY_PATH=/sf/cristallina/config/qt:/sf/op/config/qt:/sf/bd/config/qt:/sf/diag/config/qt:/sf/id/config/qt:/sf/laser/config/qt:/sf/mag/config/qt:/sf/photo/config/qt:/sf/plc/config/qt:/sf/rf/config/qt:/sf/ts/config/qt:/sf/vcs/config/qt:/sf/controls/config/qt:/ioc/modules/qt:/ioc/qt\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "(caQtDM:2000349): dbind-WARNING **: 12:21:21.231: Couldn't register with accessibility bus: Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken.\n"
     ]
    }
   ],
   "source": [
    "newport.gui()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80d92623-55a0-46c7-a8ab-60fd86be5ab9",
   "metadata": {},
   "source": [
    "# Speed settings\n",
    "Speed in units of EGU/s (engineering units/second)\n",
    "From the EPICS documentation\n",
    "\n",
    "````\n",
    "Except where specified otherwise, fields associated with the motor position and its derivatives take values in user-specified \"engineering units\", such as degrees; the engineering unit name is contained in the field EGU. Thus, generally, speeds are expressed in EGU's per second. Accelerations, however, are expressed as the number of seconds taken to accelerate to full speed. However, additional fields are provided so that the motor position can be specified in steps and the speed in revolutions per second, and so that the step size can be set by specifying the number of steps per revolution and the number of EGU's per revolution.\n",
    "````\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "3a7d603b-993a-4d8a-948c-d619c5a24445",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "speed: 12.0 mm/s\n"
     ]
    }
   ],
   "source": [
    "#slew_speed       Slew speed or velocity       .VELO\n",
    "#base_speed       Base or starting speed       .VBAS\n",
    "#acceleration     Acceleration time (sec)      .ACCL\n",
    "speed = newport._motor.slew_speed\n",
    "# newport._motor.base_speed\n",
    "# newport._motor.acceleration\n",
    "\n",
    "units = newport._motor.EGU\n",
    "\n",
    "print(f\"speed: {speed:.1f} {units}/s\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "864acb61-4ce1-47f9-a33d-c75b33e153ed",
   "metadata": {},
   "source": [
    "So we can read and set the motor speed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "e0a3b805-2b1d-4128-848b-263647a9fb2f",
   "metadata": {},
   "outputs": [],
   "source": [
    "newport._motor.slew_speed = 14"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2c80b15f-79cb-4151-9fb5-9df4cd38304b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
