# Firmware

## Motor control

Motors are controlled using the Universal Function Protocol developed by Quentin [here](https://forge.cba.mit.edu/quentin.bolsee/ufp/src/branch/main). It has two sides: the "device" (here, the stepper motor board) and the "host" (here, our laptop). The device is programmed with the functions it can run, using Arduino. The host then is able to call those functions over those protocol and the device runs them in real time.

Here I describe controlling a stepper motor unit (NEMA17 + RP2040-based control board) using UFP.

### Installing UFP stepper firmware on the device

This isn't too complicated but I prefer doing it myself rather than describing it here. I already flashed the three available board + stepper assemblies. Keep in mind that each of them has their unique MOTOR_ID - which are 1, 2 and 3 for now. I wrote it with a sharpie on each of them. When new boards come in I can program them for you.

### Installing UFP Python library on the host

Download Quentin's repo contents [here](https://forge.cba.mit.edu/quentin.bolsee/ufp/src/branch/main), open the "Python" folder in terminal, then run 
```python
pip install -e .
``` 
(don't forget the dot at the end!). Then, you're ready to communicate with your device through UFP in Python.

You can already test if the motor works using examples/stepper_motor.py. See notes below on a breakdown of the main ways the motor can be used.

### Initializing a motor

Connect your stepper board to your computer via USB. Then, identify the USB port it is connected to. In my case (Mac) it was "/dev/cu.usbmodem101". If you're on Windows, it will probably be something like "COM10". First import the ufp library

```python
import ufp
```

then initialize your device

```python
m = ufp.connect("/dev/cu.usbmodem101")   # Replace with your port name!
```

finally, supply a current to the coil (see notes below on powering up the coil)

```python
m.set_current(0.2) # replace with desired value, between 0 and 1
```

and define the current orientation of the motor

```python
m.set_position(0) # tells the board to take the current position of the stepper as the zero reference
```

These steps should normally be performed upon startup. In principle setting a constant current in the coils should work, but if we realize we need variable force/torque we can adjust it in the middle of a run.

### Supplying power to the coil

It is possible to either supply power to the coil directly from the USB, or from an external power supply (a bench supply, a battery, a voltage regulator, etc). If using the USB however, the power is quite limited. It is enough to move the naked stepper but we will have to see to which extent it provides enough torques for extrusion and XY motion.

To supply power from USB, make sure the little yellow jumper between V_USB and V_CC is plugged onto the the board. In that case, do not use current values in the program above 0.2, as that will starve the USB communication of power. If currents higher than 0.2 are needed, unplug the jumper wire, and connect a power supply between the middle pin and GND. Any voltage around 5-12 V should work on the power supply.

### Get info about the motor's state

The motor can tell you directly if it is moving or not, just call

```python
m.get_states().is_moving
```

it can also tell you wether the limit switch is pressed or not, just call

```python
m.get_states().limit
```

More generally, get_states() provides lots of info about the current state of the motor.

### Move the motor to a certain position

In the following, all units are given in terms of numbers of steps. **A full turn (360 degrees) is 200 steps.** Velocities are given in steps/s and accelerations in steps/s^2. Positions are defined relative to the zero defined upon initialization.

To turn to a target position, you also need to specify the maximum velocity and maximum acceleration to be used during the move:

```python
m.set_target_position(target_position, max_velocity, max_acceleration)
```
I recommend accelerations of 1000 normally and 5000 if you need a sudden stop. 

### Turn the motor at a constant speed

For this you just need to specify the target speed and the max acceleration.

```python
m.set_target_velocity(target_velocity, max_acceleration)
```

The motor will turn indefinitely until stopped.

### Using the limit switch

To use the limit switch, wire up the "SIG" pin on the board to the "NC" port on the limit switch, and tie GND to COM.  When the limit switch is not pressed, m.get_states().limit is False, otherwise it will be True. A typical use of the limit switch is shown in the last block inside the example code.

### Shutting down

To avoid power waste, overheating, etc, shut down the current in the coils at the end of the run

```python
m.set_current(0)
```

## Wrapper class for unidimensional motor
single_stepper_wrapper.py defines a wrapper class for a single dimensional motor, with all dimensions expressed in mm. It also includes homing on startup, robust sanity checks and error handling. single_stepper_selftest.py can be used to check correct functioning of this wrapper. simulated_stepper.py creates a virtual stepper to check against in the case where no hardware is available.

As of writing this, I only checked the wrapper against the simulated stepper. I will check tomorrow against a real stepper and report back.

## Wrapper class for coreXY gantry
xy_stage_wrapper.py defines a wrapper class for a coreXY gantry with two stepper motors (handled using the StepperMotor class defined in single_stepper_wrapper.py) with direct access to X and Y dimensions, velocities and accelerations (all in mm). Similarly, there is a testing script and a virtual XY stage simulator for testing.

Again, as of writing this, I only checked the wrapper against the simulated XY stage. I will check tomorrow against two real steppers.

For now movement is only implemented along lines but arbitrary curves can be included. Will check the interface with the software team.

### TO DO

* write a wrapper class with a single motor for the extrusion device - DONE
* write a wrapper class with two motors run in parallel for coreXY - DONE
* establish the correspondence between number of steps and physical sizes, for both the extrusion system and coreXY
* establish the coil currents needed for the appropriate torques to operate the motion systems
* establish the interface with the software team - UPDATE Nov 16: provisionally done, yet to be tested. Software modified to communicate with the wrapper classes rather than directly with UFP.