Blog 15

From BitWizard Wiki
Jump to navigation Jump to search

BETA

Stepper Motor Raspberry Pi

(connected with the user interface through a spi cable)

#!/bin/bash 

Address="bw_tool -s 50000 -a 88" 

$Address -W 42:-200:i
Target=`$Address -R 41:i`
Rot=-200
#Speed=200
#$Address -W 43:$Speed:b`  

while true; do
  Current=`$Address -R 40:i` 

  if [ "$Current" = "$Target" ]; then
    sleep 10
    $Address -W 42:$Rot:i
    Target=`$Address -R 41:i`
  fi

done

Working with stepper motor

In this post I am going to connect a stepper motor with my Arduino. I want the stepper motor to turn left and right, and I want the stepper motor to turn a certain degrees.

Before programming I first need to connect the Arduino with the stepper motor.

The hardware I used:

( I also posted in Blog 14, I connected the 7fets with an lcd + display. This is something what of course is not needed )

7fetsStepper.jpg

As you can see in the image is that the stepper Motor has 5 coloured cables, that are all together in a white cube. These cables have to get connected with the 7fets. To do this you have to get 5 male to female cables. From cable blue to orange from the stepper motor it has to be connected to pin 2,4,6,8. The red cable can be put on pin 1, all the other places in the row that are dev power. ( Places where it can gets it's voltage ) ( out 1 till 4 , works different counts 0 till 3) ( in 1 till 4 ) 7FETs


Now the programming part:

I used a script named ardemo_lcd.pde, that I changed to get it working with the stepper motor.

const int SPICLK  = 13;
const int SPIMOSI = 11;
const int SPIMISO = 12;
const int SPISS  = 10;

void SPIinit(void)
{
  pinMode (SPICLK,  OUTPUT);
  pinMode (SPIMOSI, OUTPUT);
  pinMode (SPIMISO, OUTPUT);
  pinMode (SPISS,   OUTPUT);
  digitalWrite (SPISS, 1);
   
  SPCR    =  _BV(SPE) | _BV(MSTR) | _BV(SPR1) | _BV(SPR0);
} 

char SPI(char d) 
{  // send character over SPI
  char received = 0;
  SPDR = d;
  while(!(SPSR & _BV(SPIF)));
  received = SPDR;
  return (received);
} 

void SPI_startpkt (void)
{
 digitalWrite (SPISS, 0);
}

void SPI_endpkt (void) 
{
  digitalWrite (SPISS, 1);
}

void set_var32 (unsigned char addr, unsigned char a, unsigned long v)
{
  SPI_startpkt ();
  delayMicroseconds (WAIT1);
  SPI (addr);
  delayMicroseconds (WAIT2);
  SPI (a);
  delayMicroseconds (WAIT2);
  SPI (v);
  delayMicroseconds (WAIT2);
  SPI (v>>8);
  delayMicroseconds (WAIT2);
  SPI (v>>16);
  delayMicroseconds (WAIT2);
  SPI (v>>32);
  delayMicroseconds (WAIT2);
  SPI_endpkt ();   
}

static unsigned char spi_7fet_addr = 0x88;

void setup() {
  //declare the motor pins as outputs
  SPIinit ();  
  Serial.begin(9600);
}

void loop(){
  set_var32 (0x88, 0x42, 0x50);
  delay(1000); 
}




void setup() {
  //declare the motor pins as outputs
  SPIinit ();  
  Serial.begin(9600);
}


void loop(){
  set_var32 (0x88, 0x42, 0x50);
  delay(1000); 
}

This says that at address 0x88 from the 7fets it has to go 50 further from the current position and after that it should take a 1 sec break. ( repeat and repeat itself) 0x42 is for setting the relative position. ( so in the example script it goes 50 further from the current position ) Port 42 is 32bits, don't forget about if a port is 32bits or not. ( On the DIO protocol you can find all the ports. )

Rotating Plateau

Now I want to explain how you could rotate the stepper motor a certain degrees and in which direction. This can be handy if you want to use the stepper motor as plateau for a museum, or of course other projects were you need a stepper motor.

For this I will only paste the setup + loop. ( The other parts you can just copy paste from the previous code. I didn't want to make this page too long )


//beta

 void loop(){
 set_var32 (0x88, 0x42, 0x200);
 delay(1000);  
 }

This script makes it possible that it turns 90 degrees with the register 0x200.

The values with the amount of rotation they make: 0x800 = 360 degrees 0x400 = 180 degrees 0x200 = 90 degrees 0x267 = 120 degrees

If you want another degree like for instance 45 you can just convert it. Example 800:360 = 2.2222222 * 45 = 100