Skip to content

Understand The Source Code

Ks Tan edited this page Feb 3, 2021 · 7 revisions

If you wish to make some changes in this web service, you need to understand how it work.

Files Structure

/-
 |-route.py # manage incoming traffic and map to specific function in function.py
 |-function.py #maintain logical operation, it won't directly access into physical device IO/variables to avoid break something
 |-images #no use at the moment
 |-program #no use at the moment, hope future web service allow user customise own movement routine, and store here
 |-tools/*.sh # store all the sh executable file for mac/linux users. It allow us auto QC, or become alternative training method to AR3
 |-include/-
           |- armparameters.py # keep all permanent parameter, like max/min degree, stepper motor limit, rest positions degree and etc
           |- calculation.py # i plan future linear movement will store all calculation inside here. to avoid messy in main programming file
           |- database.py # reserve future we store some runtime values into database, now this web service after quit some data just gone, like current travel track position (no encoder)
           |- Hardware.py # code using OOP, so capital file. The actual hardware execution control by here
           |- log.py # simple log function to console output. nothing much 
           |- setting.py # no use at the moment, may not need this
           |- values.py # i define some important run time variable here. It give default value, when program execute it all change in memory. However, it is good if you want to know the pattern of data

Program New Web Service

All webservice we return in json data format. You need to ensure your output match the pattern to maintain consistency.

you shall make sure when you run your service, your hand standby at emergency stop button, so you can e-stop it to prevent break something

  1. Edit route.py, add something similar as below:
@app.route('/myaction/<jointname>')
def myaction(jointname):
    result = f.runMyAction(jointname)
    #do something you want
    return result  
  1. Edit function.py, add runMyAction:
# add your function at bottom of code
def runMyAction(jointname):
    #do what you want to do, like
    rotateJoint("J1",0,"absolute")
    rotateJoint("J2",10,"move")
    moveTrack("t1", 20, "move"):
    # you can access hardware layer like below too
    hardware.goAllJointLimit([1,1,0,0,0,0])
    return log.getMsg("OK","Done")
  1. quit and restart your flask, then you can try browse to http://localhost:5000/myaction

How To Add New Hardware Layer Function

Add hardware layer function break into 2 scenario:

  1. Add function already and supported by ARCS/Teensy (Add Missing Function)
  2. Add function doesn't exists, mean you wish to create your own (Add New Function)

Add Missing Function

It is tricky work but not that difficult to make it. Below is how I do:

  1. open ARCS.py source code, search keyword at ARCS button, like Calibrate @ Rest Position, it bring me to line 6651
  2. the command attribute tell me calRobotMid is the function name to run when user press the button.
  3. search def calRobotMid, it bring me to line 5005
  4. read the source code, find actual executable serial command. Something like command = "LM".... I tried extract value
  command = "LM"+"A"+str(J1StepCur)+"B"+str(J2StepCur)+"C"+str(J3StepCur)+"D"+str(J4StepCur)+"E"+str(J5StepCur)+"F"+str(J6StepCur)+"\n"  
  print(command)
  1. save and execute ARCS.py from command line, then I can see what is the serial command.
  2. then we need understand each variable it coming from, where to store it and how to calculate it.
  3. once you proper understand it, you may change Hardware.py, please take note it is OOP file, so you need to familiar how python implement OOP, like Hardware.py use alot of self, but function.py not use any

Add Function in Hardware Level

edit Hardware.py, add the last line of code add below code to send dummy command to teensy

def doMyWork(self,arg1,arg1):
    command="ZZ01010101" #it not support by teensy, i just give example
    board = self.ser_teensy  # arduino port use self.ser_arduino
    return self.writeIO(board,command)

Use doMyWork

Edit function.py, add following function:

def myWork():
    result = hardware.doMyWork("J1",30)
    return log.showMsg(result,"do my work done")

Make doMyWork available in web service

edit route.py, add this.

@app.route('/mywork')
def runMyWork():
    result = f.runMyWork()
    return result  

Try new webservice

  1. restart Flask
  2. browse to http://localhost:5000/mywork

Add New Function

  1. it require you to write your code into Teensy, you can refer teensy source code ARCS_teensy_sketch.ino in repository
  2. add the processor you want, something like:
if(function == "ZZ")
{
   //do something you want
}
  1. Use Teensy IDE, burn new code into your teensy (Take your own risk)
  2. Do something similar according "Add Missing Function"

Important To Know Before You Code

  1. function.py accept jointname (j1/j2/j3/j4/j5/j6) from route.py, Hardware.py accept join_no (0,1,2,3,4,5) from function.py
  2. all jointsvalue, servovalues, trackvalue store in Hardware.py attribute, function.py not direct manipulate this value
  3. at the moment, arm speed is hard coded in armsparameter.py, future may change at run time (not now)
  4. I notice firmware in teensy doesn't not block any movement beyond limit switch. It is python side responsibility to validate value whether acceptable range. The limit switch only function under calibration, no more than that. (I not so satisfy firmware not control it, but I not confident to changes Teensy cause I worry it break something)