Difference between revisions of "Blog 19"

From BitWizard Wiki
Jump to navigation Jump to search
Line 137: Line 137:


You can now easily talk to both 7FETs.
You can now easily talk to both 7FETs.

== 2 Wheeled 7FETs Car: Arduino Version ==

In this script I will show how to let 2 7FETs that are connected with a stepper motor drive forward.
If you scroll down you will also see how to go left, right and backwards.

static unsigned char spi_7fet_addr = 0x88;
static unsigned char spi_7fet_addrL = 0x70;
//Time to pause rotating
static unsigned long Pause = 10000;
//Amount you want the stepper motor to rotate (0x200 = 25% 0x400 = 50% 0x800= 100% )
static unsigned long Rot = -0x200;
static unsigned long Rot2 = -0x200;
void setup() {
//declare the motor pins as outputs
SPIinit ();
Serial.begin(9600);
char buf[32];
unsigned long RTar;
unsigned long RCur;
unsigned long LTar;
unsigned long LCur;
//Makes the current(0x40) + current(0x41) zero. Handy if the current + target have a too big difference.
set_var32(0x88, 0x40, 0x00);
set_var32(0x88, 0x41, 0x00);
set_var32(0x70, 0x40, 0x00);
set_var32(0x70, 0x41, 0x00);
//Change the step delay //50 is 4 times as fast as he normally goes (200)
set_var(0x88, 0x43, 200);
set_var(0x70, 0x43, 200);
RTar = get_var32 (0x88, 0x41);
sprintf (buf, "RIGHT FTarget: %08lx\r\n", RTar );
Serial.write (buf);
RCur = get_var32 (0x88, 0x40);
sprintf (buf, "RIGHT Fcur: %08lx\r\n", RCur);
Serial.write (buf);
LTar = get_var32 (0x70, 0x41);
sprintf (buf, "LEFT FTarget: %08lx\r\n", LTar );
Serial.write (buf);
LCur = get_var32 (0x70, 0x40);
sprintf (buf, "LEFT Fcur: %08lx\r\n", LCur);
Serial.write (buf);
}
void loop()
{
unsigned long RF;
unsigned long RCur;
unsigned long LCur;
unsigned long LF;
char buf[32];
set_var32(0x88, 0x42, Rot);
set_var32(0x70, 0x42, Rot2);
RF = get_var32 (0x88, 0x41);
sprintf (buf, " RTarget: %08lx", RF);
Serial.write (buf);
LF = get_var32 (0x70, 0x41);
sprintf (buf, " LTarget: %08lx\r\n", LF);
Serial.write (buf);
do {
delay(200);
RCur = get_var32 (0x88, 0x40);
sprintf (buf, " Rcur: %08lx", RCur);
Serial.write (buf);
LCur = get_var32 (0x70, 0x40);
sprintf (buf, " Lcur: %08lx\r\n", LCur);
Serial.write (buf);
} while (RCur != RF);
delay(Pause);
}

Go backwards( You have to add a minus before the rotation value.(You can also add the minus in the loop before rot)):
outside void setup & loop:
static unsigned long Rot = -0x400;
static unsigned long Rot2 = -0x400;

in void setup:
set_var(0x88, 0x43, 200);
set_var(0x70, 0x43, 200);

Going left:
outside void setup & loop:
static unsigned long Rot = 0x400;
static unsigned long Rot2 = 0x200;

in void setup:
set_var(0x88, 0x43, 100);
set_var(0x70, 0x43, 200);
Going Right:
outside void setup & loop:
static unsigned long Rot = 0x200;
static unsigned long Rot2 = 0x400;

in void setup:
set_var(0x88, 0x43, 200);
set_var(0x70, 0x43, 100);

Revision as of 13:43, 24 November 2015

2 Wheeled car: Arduino Version

#define Motor 0x48

#include <Wire.h> 

//Time to pause rotating
//static unsigned long Pause = 10000;
static unsigned long Speed = 40;

void setup()
{
  Wire.begin(); // wake up I2C bus
  Serial.begin(9600);
}

byte get_var(byte address, byte reg)
{
  byte value;   
  Wire.beginTransmission(address);  
  Wire.write(reg);
  Wire.endTransmission(); 
  delayMicroseconds (10);
  Wire.requestFrom(Motor, 1);
  value = Wire.read();
  return value;
} 

void set_var(byte address, byte reg, byte value)
{
  Wire.beginTransmission(address); // transmit to motor     
  delayMicroseconds  (10);
  Wire.write(reg);
  delayMicroseconds  (10);
  Wire.write(value);   
  Wire.endTransmission();    // stop transmitting
} 

void loop()
{
  unsigned long AddressA;
  unsigned long AddressB;  

  char buf[32];
  set_var(0x48, 0x21, Speed); 
  set_var(0x48, 0x31, Speed);  

  AddressA = get_var(0x48, 0x21);
  sprintf (buf, "Speed:  A:%d ", AddressA);
  Serial.write (buf); 

  AddressB = get_var(0x48, 0x31);
  sprintf (buf, " B:  %d\r\n", AddressB);
  Serial.write (buf); 
 
 delay(500);

//  delay(Pause);
}

This is the list I have made for the Raspberry Pi Version, what also counts for the Arduino version(If your car is switched to the other side this can of course be the opposite):

  • # X is forwards - Y is backwards
  • #Wheels at front
  • #20 A backwards
  • #21 A forwards
  • #22 A stop
  • #30 B backwards
  • #31 B forwards
  • #32 B stop

You can also check the motor protocol, for more info.

In my example script we go forward with 0x21 + 0x31 having the same value. For other movements you should have(If you car is flipped this can of course be the opposite!):

To go backwards:

  set_var(0x48, 0x20, Same Speed value); 
  set_var(0x48, 0x30, Same Speed value); 

To go left:

  set_var(0x48, 0x21, High Speed value); 
  set_var(0x48, 0x31, Low Speed value); 

To go right:

  set_var(0x48, 0x21, Low Speed value); 
  set_var(0x48, 0x31, High Speed value); 

To stop the car:

  set_var(0x48, 0x21, Speed value doesn't matter); 
  set_var(0x48, 0x31, Speed value doesn't matter);

To rotate to the left:

  set_var(0x48, 0x21, Same Speed value); 
  set_var(0x48, 0x30, Same Speed value); 

To rotate to the right:

  set_var(0x48, 0x20, Same Speed value); 
  set_var(0x48, 0x31, Same Speed value);

If you do this don't forget to add: static unsigned long Speed2 = "Put here your value";


How to change SPI address, when the same addresses are connected on 1 port (Raspberry Pi)

Here I want to tell you quickly how to make it possible to connect an 7Fets attached ot an other 7Fets connected to the raspberry Pi. When you normally connect on SPI0 and scan:

bw_tool -S -D /dev/spidev0.0

You will see:

88: spi_7fets 1.1

This is something you don't want, because now he only sees 1 7FETs. And with the car you of course want to make it possible to let the car drive different directions. To do this you first have to remove the 7FETs you got connected to the other 7FETs that is connected to the port. Now you should change the address of the connected 7FETs. ( If you would have both of the 7FETs connected both the addresses will change to the new one ) To change the address you have to do the command:

bw_tool -S -D /dev/spidev0.0 -a 88 -w f1:55 f2:aa f0:70

( If you are using I2C, that works the same only you have to change -S to -I and /dev/spidev0.0 to /dev/spi-i2c-1 )

What the code does:

f1:55

opens the address

f2:aa 

allows changing the address

f0:70

Is me changing the address to 70. ( You can of course give your own value )

On User Interface you can read more about the commands.

If you then connect the other 7FETs on the other 7FETs again and scan again you will get as result: 70: spi_7fets 1.1 88: spi_7fets 1.1

You can now easily talk to both 7FETs.

2 Wheeled 7FETs Car: Arduino Version

In this script I will show how to let 2 7FETs that are connected with a stepper motor drive forward. If you scroll down you will also see how to go left, right and backwards.

static unsigned char spi_7fet_addr = 0x88;
static unsigned char spi_7fet_addrL = 0x70;

//Time to pause rotating
static unsigned long Pause = 10000;
//Amount you want the stepper motor to rotate (0x200 = 25%  0x400 = 50% 0x800= 100% )
static unsigned long Rot = -0x200;
static unsigned long Rot2 = -0x200;

void setup() {
  //declare the motor pins as outputs
  SPIinit ();  
  Serial.begin(9600);
  char buf[32];
  unsigned long RTar;
  unsigned long RCur; 
  unsigned long LTar;
  unsigned long LCur; 

  //Makes the current(0x40) + current(0x41) zero. Handy if the current + target have a too big difference. 
  set_var32(0x88, 0x40, 0x00);
  set_var32(0x88, 0x41, 0x00);
  set_var32(0x70, 0x40, 0x00);
  set_var32(0x70, 0x41, 0x00);

  //Change the step delay //50 is 4 times as fast as he normally goes (200)
  set_var(0x88, 0x43, 200);
  set_var(0x70, 0x43, 200);

  RTar = get_var32 (0x88, 0x41);
  sprintf (buf, "RIGHT FTarget: %08lx\r\n", RTar );
  Serial.write (buf);

  RCur = get_var32 (0x88, 0x40);
  sprintf (buf, "RIGHT Fcur:    %08lx\r\n", RCur);
  Serial.write (buf);

  LTar = get_var32 (0x70, 0x41);
  sprintf (buf, "LEFT FTarget: %08lx\r\n", LTar );
  Serial.write (buf);

  LCur = get_var32 (0x70, 0x40);
  sprintf (buf, "LEFT Fcur:    %08lx\r\n", LCur);
  Serial.write (buf);

}

void loop()
{
  unsigned long RF;
  unsigned long RCur;
  unsigned long LCur;
  unsigned long LF;

  char buf[32];
  set_var32(0x88, 0x42, Rot); 
  set_var32(0x70, 0x42, Rot2); 
 
  RF = get_var32 (0x88, 0x41);
  sprintf (buf, " RTarget:  %08lx", RF);
  Serial.write (buf);

  LF = get_var32 (0x70, 0x41);
  sprintf (buf, " LTarget:  %08lx\r\n", LF);
  Serial.write (buf);

  do {
    delay(200);

    RCur = get_var32 (0x88, 0x40);
    sprintf (buf, " Rcur:     %08lx", RCur);
    Serial.write (buf);

    LCur = get_var32 (0x70, 0x40);
    sprintf (buf, " Lcur:     %08lx\r\n", LCur);
    Serial.write (buf);

  } while (RCur != RF);

  delay(Pause);  
}

Go backwards( You have to add a minus before the rotation value.(You can also add the minus in the loop before rot)): outside void setup & loop: static unsigned long Rot = -0x400; static unsigned long Rot2 = -0x400;

in void setup:

 set_var(0x88, 0x43, 200);
 set_var(0x70, 0x43, 200);

Going left: outside void setup & loop: static unsigned long Rot = 0x400; static unsigned long Rot2 = 0x200;

in void setup:

 set_var(0x88, 0x43, 100);
 set_var(0x70, 0x43, 200);

Going Right: outside void setup & loop: static unsigned long Rot = 0x200; static unsigned long Rot2 = 0x400;

in void setup:

 set_var(0x88, 0x43, 200);
 set_var(0x70, 0x43, 100);