{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## driver Jupyter notebook for STEPCRAFT navigation, images acquisition, and automatisation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "#load the required libraries\n",
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "import sys,json\n",
    "FlumeAutomationLib='../lib/'           #path to OctControl.pyd, GRBLserver.py, ImageAcquisition\n",
    "if not FlumeAutomationLib in sys.path:\n",
    "    sys.path.append(FlumeAutomationLib)\n",
    "\n",
    "import OctControl\n",
    "import GRBLserver\n",
    "from ImageAcquisition import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#connect the positioning device\n",
    "stp=GRBLserver.grbl(conName='sberpc17.epfl.ch',url='/stepcraft') #port and conname are port and IP address of the GRBL server (see GRBLserver.py)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#calibrate the positioning device\n",
    "stp.homing() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "OctControl.init()   #initialize OCT, NB ThorImage software has to be closed before"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#define a temporary set of configuration parameters\n",
    "params={\n",
    "  \"ganymede\": 2,\n",
    "  \"probe\": 1,\n",
    "  \"nAscans\": 10,\n",
    "  \"nBscans\": 10,\n",
    "  \"nCscans\": 1024,\n",
    "  \"X\": 10.00,\n",
    "  \"Y\": 10.00,\n",
    "  \"refr\": 1.33,\n",
    "  \"avg_Ascans\": 4,\n",
    "  \"scanspeed\": 2,\n",
    "  \"angle\": 0,\n",
    "  \"ACC\": 0,\n",
    "  \"path\": \"../Measurments/%Y-%m-%d %H_%M_%S\",\n",
    "  \"exportRAWGrayscale8bit\": \"8bit.raw\",\n",
    "  \"exportSRM\": \"data.srm\",\n",
    "  \"colorBoundaries\":[0.0,90.0]\n",
    "\n",
    "}\n",
    "\n",
    "OctControl.setParameters(**params)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#load defalut configuration parameters\n",
    "params=json.load(open(\"../lib/config.json\"))  #path to config.json, containing the image acquisition parameters, to be set according to your needs\n",
    "OctControl.setParameters(**params)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " commands to navigate the STEPCRAFT:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "stp.actualizeStatus()\n",
    "stp.status"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "stp.moveTo(x=-400,y=-2,z=-1) #move stepcraft in position (x=-500,y=-2,z=-1)\n",
    "stp.waitFinish()\n",
    "print(stp.status)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "commands to acquire OCT scans:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "makeScan() #OCT scan in the current position of the CNC device, using configurations settings previously set in params"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "makeScan(params={\"nBscans\":10,\"nAscans\":10}) #OCT scan in the current position of the CNC device, the number of Bscans can be modified here directly"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "makeMoveScan(pos=(-463.5, -2, -2), params={}) #OCT scan in position with coordinates(-463.5, -11.5, -40)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#set here the coordinates of the positions of interest\n",
    "positions={\n",
    "    \"pos1\":(-463.5,-11.5,-2),\n",
    "    \"pos2\":(-463.5,-101.5,-2)} "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "for p in positions:\n",
    "    makeMoveScan(stp_position=p,positions=positions) #make OCT scan in position pos1, as save it in the folder selected in params"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "G0Z-0.500\n",
      "G0X-470.500Y-18.500\n",
      "G0Z-2.000\n",
      "G0Z-0.500\n",
      "G0Z-0.500\n",
      "G0X-463.500Y-18.500\n",
      "G0Z-2.000\n",
      "G0Z-0.500\n",
      "G0Z-0.500\n",
      "G0X-456.500Y-18.500\n",
      "G0Z-2.000\n",
      "G0Z-0.500\n",
      "G0Z-0.500\n",
      "G0X-470.500Y-11.500\n",
      "G0Z-2.000\n",
      "G0Z-0.500\n",
      "G0Z-0.500\n",
      "G0X-463.500Y-11.500\n",
      "G0Z-2.000\n",
      "G0Z-0.500\n",
      "G0Z-0.500\n",
      "G0X-456.500Y-11.500\n",
      "G0Z-2.000\n",
      "G0Z-0.500\n",
      "G0Z-0.500\n",
      "G0X-470.500Y-4.500\n",
      "G0Z-2.000\n",
      "G0Z-0.500\n",
      "G0Z-0.500\n",
      "G0X-463.500Y-4.500\n",
      "G0Z-2.000\n",
      "G0Z-0.500\n",
      "G0Z-0.500\n",
      "G0X-456.500Y-4.500\n",
      "G0Z-2.000\n",
      "G0Z-0.500\n"
     ]
    }
   ],
   "source": [
    "multipleScan('pos1', n=(3,3), params={\"nBscans\":10,\"nAcans\":10},positions=positions) #acquire 9 images in a square tiling pattern (3 by 3), overlapping by 30%, \n",
    "                                #centered in position pos1\n",
    "                                #in order to change overlap between images, \n",
    "                                #change the function multipleScan in the library ImageAcquisition.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tic()  #take 3 by 3 tiles in each position defined in the dictionary 'positions'\n",
    "for p in positions:\n",
    "    multipleScan(p, n=(3,3),positions=positions)\n",
    "toc()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "OctControl.close() #close the OCT device"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
