Driving a Step Motor with a Teensy Board Using PsychToolbox

42 Ansichten (letzte 30 Tage)
연주
연주 am 26 Nov. 2024 um 5:13
Beantwortet: Aravind am 27 Nov. 2024 um 5:19
I would like to make the following RRST equipment and conduct an experiment.
The experiment was designed using psychtoolbox. Sending commands from MATLAB should drive the stepmotor through the teensy board.
The main program works, but the step motor does not move. It was confirmed that there was nothing wrong with the motor itself. Can I use MATLAB to drive a teensy board-linked step motor? I’m wondering if the serial communication part is wrong.
function [moved, currPosition] = moveResp(respDevice, newPosition, unitscale)
rescale = 1; % rescale motor step values
try
if rescale
% Rescale new position input to motor step value
[motorStepVal] = scale2motorstep(newPosition, unitscale);
else
motorStepVal = newPosition;
end
moveToHere = strcat(sprintf('%03d', motorStepVal));
% Move & update new current position output
writeline(respDevice, moveToHere)
currPosition = newPosition;
moved = 1;
catch
currPosition = NaN;
moved = 0;
end
end
+) arduino code
/*==========================================================================
===========================================================================*/
#include “TeensyStep.h”
#define endstopPin 7
#define enablePin 8
#define dirPin 9
#define stepPin 10
#define MAXPOSITION 37000
long initial_homing=-200;
//Serial interface setup
const byte numChars = 90;
char receivedChars[numChars];
boolean newData = false;
long stepperPosition = 0;
Stepper motor(stepPin, dirPin);
StepControl controller; // Use default settings
void setup()
{
Serial.begin(9600);
motor
.setAcceleration(10000) // 20 000
.setMaxSpeed(16000);
// Setting up Endstop pin
pinMode(endstopPin,INPUT_PULLUP);
// Setting up enable pin and enable the driver
pinMode(enablePin,OUTPUT);
digitalWrite(enablePin,LOW);
// Setup LED pin for debugging
pinMode(LED_BUILTIN, OUTPUT);
// Home the stepper
homeStepper();
}
void loop()
{
recvWithEndMarker();
showNewNumber();
motor.setTargetAbs(stepperPosition);
// Set the target position:
if(stepperPosition <= MAXPOSITION && stepperPosition >= 0)
{
motor.setTargetAbs(stepperPosition);
controller.move(motor); // Do the move
}
else if (stepperPosition > MAXPOSITION)
{
stepperPosition = MAXPOSITION;
Serial.print("Out of range! Setting position to MAXPOSITION: ");
Serial.println(stepperPosition);
motor.setTargetAbs(stepperPosition);
controller.move(motor); // Do the move
}
else if (stepperPosition < 0)
{
homeStepper();
}
}
// Serial receive function
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = ‘\n’;
char rc;
if (Serial.available() > 0) {
rc = Serial.read();
Serial.print((char)rc);
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewNumber() {
if (newData == true) {
Serial.print("Current position: ");
Serial.println(stepperPosition);
stepperPosition = 0; // new for this version
stepperPosition = atol(receivedChars); // new for this version
Serial.print("New position: ");
Serial.println(receivedChars);
newData = false;
}
}
// Homing routine
void homeStepper(){
Serial.println(“Fast Homing begins:”);
int endstopStatus = digitalRead(endstopPin);
Serial.println(endstopStatus);
while (digitalRead(endstopPin)) { // Make the Stepper move CCW until the switch is activated
motor.setTargetRel(initial_homing); // Set the position to move to
controller.move(motor); // Start moving the stepper
delay(4);
}
Serial.println(“Fast Homing ended”);
endstopStatus = digitalRead(endstopPin);
Serial.println(endstopStatus);
Serial.println(“Stepping forward”);
Serial.println( motor.getPosition());
motor.setTargetRel(2000);
controller.move(motor);
delay(600);
endstopStatus = digitalRead(endstopPin);
Serial.println("Re-homing ");
Serial.println(endstopStatus);
Serial.println( motor.getPosition());
while (digitalRead(endstopPin)) { // Make the Stepper move CCW until the switch is activated
motor.setTargetRel(-5); // Set the position to move to
controller.move(motor); // Start moving the stepper
Serial.println( motor.getPosition());
delay(3);
}
endstopStatus = digitalRead(endstopPin);
Serial.println("Homed ");
Serial.println(endstopStatus);
stepperPosition = 0;
motor.setPosition(0);
}

Antworten (2)

Angelo Yeo
Angelo Yeo am 26 Nov. 2024 um 10:55
Teensy is not supported in MATLAB R2023b. It is supported in MATLAB R2024b and later. For a full list of supported hardware, please refer to the link below:
  1 Kommentar
연주
연주 am 27 Nov. 2024 um 0:22
Thank you for your answer.
The paper I am referring to was published in 2021.
I use Teensy 3.1. It appears to control the teensy board in a different way.

Melden Sie sich an, um zu kommentieren.


Aravind
Aravind am 27 Nov. 2024 um 5:19
While controlling a stepper motor directly through MATLAB is not feasible, you can achieve this using Simulink. Starting with MATLAB R2024a, the Simulink Support Package for Arduino Hardware allows you to deploy models to Teensy boards. More information is available here: https://www.mathworks.com/hardware-support/arduino.html#simulink. This page also provides guidance on setting up the core libraries for the Teensy board in MATLAB R2024a.
Once the core libraries are set up, you can use the "IO Device Builder" app to incorporate external Teensy C++ libraries for stepper motor control and create a Simulink block for this purpose. Detailed information about the "IO Device Builder" app can be found here: https://www.mathworks.com/help/releases/R2023b/supportpkg/arduino/io-device-builder.html. The block you create can be deployed to a Teensy board using code generation, enabling you to control the stepper motor from Simulink.
Since MATLAB and Simulink are well integrated, you can run and control the Simulink model from MATLAB code, allowing you to indirectly control the stepper motor through MATLAB.
Here are some resources to help you get started with the "IO Device Builder" app:
  1. Introduction to the "IO Device Builder" app: https://www.mathworks.com/help/releases/R2023b/supportpkg/arduino/ug/io-device-builder.html
  2. Example with the DHT11 sensor: https://www.mathworks.com/help/releases/R2023b/supportpkg/arduino/ref/dht11-relative-humidity-temperature-io-device-builder.html
  3. Example with the ADXL343 sensor: https://www.mathworks.com/help/releases/R2023b/supportpkg/arduino/ref/adxl343-read-acceleration-io-device-builder.html
  4. Video tutorial on creating custom sensor blocks for Arduino in Simulink: https://www.mathworks.com/videos/how-to-build-custom-sensor-blocks-for-arduino-in-simulink-1704432957396.html
I hope this helps!

Produkte


Version

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by