Setting up MATLAB Arduino Connection
Jump to navigation
Jump to search
MATLAB Setup
- You will need the MATLAB Arduino support package.
- Use the "Get support package" button in the right sidebar. It will download a .mlpkginstall file; open it with MATLAB to install the package.
- You must have the DSP toolbox. If you don't, install it from the MATLAB website first. All of the other dependencies will be automatically installed.
Connect Arduino
- Type arduinosetup at the command line to begin the setup process. The server will be installed on the Arduino.
- At the "Choose Connection Type" screen, pick "USB".
- On the "Upload Arduino Server" screen, pick "Due" for the board type.
- For the libraries, pick at least RotaryEncoder, and be sure to upload the server program by clicking the "Program" button.
Uploading the server will wipe the existing code on the Arduino.
- At the end of the setup process, you will be instructed to type a=arduino. This will assign the Arduino object to a variable a. You will pass the variable a as the first argument of the Arduino-related MATLAB functions. If you don't, MATLAB won't know which Arduino you're talking about.
- After the first time, you can just type a=arduino if you want to use the same settings as last time, and MATLAB will upload and verify the server code automatically.
- If you're getting errors because the port is busy, restart MATLAB and try again. If that doesn't work, start the Arduino IDE and upload the basic "blink" example.
- The front motor may spin; to prevent this, include analogWrite(9,0) in your setup function to stop it.
NOTE! After this point, when the Arduino is connected, you MUST turn off the motors before shutting down MATLAB. The motors WILL spin rapidly unless the e-stop is pushed down and/or both side switches are in the "off" position.
- The pin assignments are all the same as they are in the Arduino code: for example, 22/35/36 for the red/yellow/blue LEDs, respectively. You can try blinking the yellow LED with the following code:
for i = 1:10 writeDigitalPin(a, 'D35', 0); pause(0.5); writeDigitalPin(a, 'D35', 1); pause(0.5); end
Note the "D" in front of the pin name, which is required.
- Certain pins are capable of certain types of output: analog/PWM or digital, for example. You will get an error message if you try to send the wrong type of signal. In some cases, MATLAB will be unable to figure out what sort of signal it's supposed to send. Use configurePin(a,'D13','Unset') to reset the pin configuration for pin 13; leave out the third argument to print out the current configuration, which may help with debugging.
Control Front Motor
- You can control the front motor with the same pins as in the Arduino code.
- Digital pin D46 controls the direction.
- Set it to 1 for counterclockwise (when viewed from above the bike) and 0 for clockwise, using the writeDigitalPin function above.
- Analog pin D9 controls the angular velocity. Set it to a floating point value between 0 (no motion) to 1 (full speed, do not attempt).
- Motion will start in the range 0.10 to 0.13, depending on the battery.
- To write to D9, you can use writePWMDutyCycle, which takes its arguments in the same order as writeDigitalPin.
Read Encoder
- You will need to use digital pins D2 and D13, which take the "A" and "B" signals for our quadrature encoder.
- Create an encoder object with encoder=rotaryEncoder(a,'D2','D13').
- You might get an error message saying you need to configure a pin. In that case, reset the pin configuration of the pin mentioned in the error message using the above instructions.
- The current speed can be read out (after setting the number of pulses per revolution) using readSpeed(encoder).
Troubleshooting
If the motor isn't on, try the standard troubleshooting steps.